Monday, July 14, 2008

Reading and writing on files

Many times the there is the needing to save the data.
There are two ways to do it:
- Saving the data in a file (.txt, .dat, ecc...)
- Saving the data in a database (Access, Mysql, ecc...)
Now we analyze the first case, saving the data in a file.
It is more convenient to save data in a text file when you have to save a little quantity of data, on the contrary when you have to save, and therefore interact with a great quantity of data it is better to use a database. In this article we want to see how to read and write in a file named "pippo.txt".

Writing on files

Before writing on a file we must open it, therefore we use the following expression:
$write_file=fopen("pippo.txt","w");
$write_file is called pointer and it is used to open the file by the istruction fopen that has, as first argument, the name of the file and, as second argument, the modality to open it.
"w" means that we want to open the file pippo.txt to write on it.
Other modalities to open a file are the following:
"r" -> opens the file in reading modality
"r+" -> opens the file in reading+writing modality
"w" -> opens the file in writing modality
"w+" -> opens the file in reading+writing modality
"a" -> opens the file in writing modality and insert the pointer at the end of file ("w" at the start)
"a+" -> opens the file in reading+writing modality
Now we want to write in the file the string "Hello!!!", therefore we use the following code:
$string="Hello!!!";
fwrite($write_file,$string);
fclose($write_file);
The istruction fwrite writes the string $string in the file pippo.txt. fwrite has, as first argument, the pointer that we used to open the file and, as second argument, the string that we want to write on the file. fclose needs to close the interaction with the file.

Reading on files

Now we want to read the string that we have written in the file.
To do it, before all, we must open the file in reading modality, therefore we write the following line of code:
$read_file=fopen("pippo.txt","r");
$read_file is a pointer that use the fopen instruction to open the file pippo.txt in reading modality this time.
To read the content of file we must write the following code:
$dim_file=filesize("pippo.txt");
$content=fread($read_file,$dim_file);
fclose($read_file);
T o read the complete content of the file we must know his dimension, therefore we use the instruction filesize that calculates the dimension (in bytes) of the file that has in his argument. The instruction fread open the file to read his content. fread has, as first argument, the pointer to open the file in reading modality and, as second argument, the portion of file that we want to read, in this case all the file. Then fread stores the content of file in the variable $content.
Even in this case fclose closes the interaction with the file.
If we want to print on screen the content of the file pippo.txt, we need to write easily:
echo $content;
On the screen it will be displayed the string Hello!!!

Complete code

The complete code to read and write on a file, as seen in this article, is the following, remembering to you that the simbols // are used to introduce a comment in php:
// String to write on file
$string="Hello!!!";
// Writing on file
$write_file=fopen("pippo.txt","w");
fwrite($write_file,$string);
fclose($write_file);
// Reading on file
$raed_file=fopen("pippo.txt","r");
$dim_file=filesize("pippo.txt");
$content=fread($read_file,$dim_file);
fclose($read_file);
// Printing on screen the content of file
echo $contenuto;

Article written by the webmaster of Tuttophp

Words detecting

This time I want to build a function that can detect undesired words contained in a message. This function could be very usefull when you want to build a guestbook or a chat. Infact you can prevent the displaying of messages containing undesired words. But now we atake a look to the code. The function will be called 'check'.
<?
function check($text)
{
$undesired_words=array("cat","dog");
$result=0;
$dim=count($undesired_words);
for ($i=0;$i<$dim;$i++)
{
if (stristr($text,$undesired_words[$i]))
{
$result=1;
}
}
return $result;
}

$message="Today I went away with my dog for a walk";
if (check($message)) echo "There are undesired words in the message"; else echo "All it's ok";
?>

A comment on the code:
The function chek has as argument the message that we want to analyze. We build an array called undesired_words containing the undesired words, in this case thay are cat and dog. The variable $result is a flag whos value is 1 if it was found an undesired word else her value is 0. The cycle for scans all the words in the array undesired_words while the php function stristr detect if the word is contained in the message. To control the validity of the function 'check' we insert into the variable $message a message and with an if we control the validity of the 'check' function.
This example can be used as a base to build a system that block the displaying of messages in your php applications.

Article written by the webmaster of Tuttophp

Script testing in local machine

As I already said in the article "What is Php", the server elaborates the php codes producing html code, as shown below:

How we can run a php page in our computer if php needs a server?
In the web, it is possible to download local servers, that is to say programs that simulate a server in your computer. One of them is Apache, that is distributed always with new versions.
If your php script require a database, for example Mysql, you must install in your computer Mysql database and if you want Phpmyadmin that make easier your interaction with the database Mysql.
To conclude this article, to run a php script on your computer
you need the following programs:
- a local server (es: Apache)
- a version of Php (files of Php)
- a database (es: Mysql)
There are packages like EasyPhp or Php Triad that contain Apache,a version of Php,Mysql and Phpadmin.
Good Work!

Article written by the webmaster of Tuttophp

What is php

Php is a web-oriented server-side programming language.
You can use this language to create many internet-applications,
for example guestbooks, polls, counters, forums e many other things....
What means server-side? Php is a server-side language because the php-code must be elaborated by the server, that produces a page entirely in html.that you see in your browsers. Grafically this situation
could be represented like below:Php is a web-oriented server-side programming language.
You can use this language to create many internet-applications,
for example guestbooks, polls, counters, forums e many other things....
What means server-side? Php is a server-side language because the php-code must be elaborated by the server, that produces a page entirely in html.that you see in your browsers. Grafically this situation
could be represented like below:

To make a concrete example, I want to print on screen the word 'hello'. Between the tags <body></body> I write the following php code:
<?php echo "hello"; ?>
The server find in the page the php code( delimited by tags <?php ?> ), elaborates this code and produces a page entirely in html where,
between tag <body></body>, there is the word 'hello'.

Article written by the webmaster of TuttoPhp