3. The basics of PHP
3.1. A first example
Below is a program demonstrating the initial features of PHP.
3.1.1. The program (exemple_01)
<?php
// this is a comment
// variable used without being declared
$nom = "dupont";
// a screen display
print "nom=$nom\n";
// an array with elements of different types
$tableau = array("un", "deux", 3, 4);
// its number of elements
$n = count($tableau);
// a loop
for ($i = 0; $i < $n; $i++)
print "tableau[$i]=$tableau[$i]\n";
// initialize 2 variables with the contents of an array
list($chaine1, $chaine2) = array("chaine1", "chaine2");
// concatenation of the 2 strings
$chaine3 = $chaine1 . $chaine2;
// result display
print "[$chaine1,$chaine2,$chaine3]\n";
// use function
affiche($chaine1);
// the type of a variable can be known
afficheType($n);
afficheType($chaine1);
afficheType($tableau);
// the type of a variable can change at runtime
$n = "a changé";
afficheType($n);
// a function can return a result
$res1 = f1(4);
print "res1=$res1\n";
// a function can return a table of values
list($res1, $res2, $res3) = f2();
print "(res1,res2,res3)=[$res1,$res2,$res3]\n";
// we could have retrieved these values in a table
$t = f2();
for ($i = 0; $i < count($t); $i++)
print "t[$i]=$t[$i]\n";
// testing
for ($i = 0; $i < count($t); $i++)
// displays only channels
if (getType($t[$i]) == "string")
print "t[$i]=$t[$i]\n";
// other tests
for ($i = 0; $i < count($t); $i++){
// displays only integers >10
if (getType($t[$i]) == "integer" and $t[$i] > 10)
print "t[$i]=$t[$i]\n";
}
// une boucle while
$t = array(8, 5, 0, -2, 3, 4);
$i = 0;
$somme = 0;
while ($i < count($t) and $t[$i] > 0) {
print "t[$i]=$t[$i]\n";
$somme+=$t[$i]; //$somme=$somme+$t[$i]
$i++; //$i=$i+1
}
print "somme=$somme\n";
// end of program
exit;
//----------------------------------
function affiche($chaine) {
// displays $chaine
print "chaine=$chaine\n";
}
//----------------------------------
function afficheType($variable) {
// displays the type of $variable
print "type[$variable]=" . getType($variable) . "\n";
}
//----------------------------------
function f1($param) {
// adds 10 to $param
return $param + 10;
}
//----------------------------------
function f2() {
// returns 3 values
return array("un", 0, 100);
}
?>
The results:
Comments
- Line 5: In PHP, variable types are not declared. These variables have a dynamic type that can change over time
- $nom represents the variable with the identifier "name"
- line 8: to write to the screen, you can use the print statement or the echo statement
- Line 11: The keyword array is used to define an array. The variable $nom[$i] represents the element $i of the array $tableau.
- Line 14: The function count($tableau) returns the number of elements in the array $tableau
- line 18: strings are enclosed in double quotes " or single quotes '. Inside double quotes, the variables $variable are evaluated, but not inside single quotes.
- line 21: the list function allows you to gather variables into a list and assign a value to them with a single assignment operation. Here, $chaine1="string1" and $chaine2="string2".
- line 24: the . operator is the string concatenation operator.
- line 88: the keyword function defines a function. A function may or may not return values using the return statement. The calling code can ignore or retrieve the results of a function. A function can be defined anywhere in the code.
- line 90: the predefined function getType($variable) returns a string representing the type of $variable. This type may change over time.
- Line 79: The predefined function exit terminates the script.
3.2. Variable scope
3.2.1. Program 1 (exemple_02)
<?php
// variable scope
function f1() {
// we use the global variable $i
$i = &$GLOBALS["i"];
$i++;
$j = 10;
print "f1[i,j]=[$i,$j]\n";
}
function f2() {
// we use the global variable $i
$i = &$GLOBALS["i"];
$i++;
$j = 20;
print "f2[i,j]=[$i,$j]\n";
}
function f3() {
// we use a local variable $i
$i = 4;
$j = 30;
print "f3[i,j]=[$i,$j]\n";
}
// tests
$i = 0;
$j = 0; // these two variables are known to a function f via the table $GLOBALS
f1();
f2();
f3();
print "test[i,j]=[$i,$j]\n";
?>
The results:
Comments
- Lines 29–30: define two variables, $i and $j, in the main program. These variables are not known within the functions. Thus, in line 9, the variable $j in function f1 is a local variable of function f1 and is different from the variable $j in the main program. A function can access a variable $variable from the main program via an array of global variables called $GLOBALS. Line 7, the notation $GLOBALS["i"] refers to the global variable $i in the main program. If we write
a local variable $i is assigned the value of the global variable $i. These are two different variables, and modifying the local variable $i will not modify the global variable $i. The notation
means that the local variable $i has the same memory address as the global variable $i. Manipulating the local variable $i is therefore equivalent to manipulating the global variable $i.
3.2.2. Program 2 (exemple_03)
<?php
// the scope of a variable is global to code blocks
$i = 0; {
$i = 4;
$i++;
}
print "i=$i\n";
?>
The results:
Comments
In some languages, a variable defined within curly braces has the scope of those braces: it is not known outside of them. The results above show that this is not the case in PHP. The variable $i defined on line 5 inside the curly braces is the same as the one used on lines 4 and 8 outside of them.
3.3. Arrays
3.3.1. Classic one-dimensional arrays (exemple_04)
<?php
// classic paintings
// initialization
$tab1 = array(0, 1, 2, 3, 4, 5);
// routes - 1
print "tab1 a " . count($tab1) . " éléments\n";
for ($i = 0; $i < count($tab1); $i++)
print "tab1[$i]=$tab1[$i]\n";
// routes - 2
print "tab1 a " . count($tab1) . " éléments\n";
reset($tab1);
while (list($clé, $valeur) = each($tab1))
print "tab1[$clé]=$valeur\n";
// add elements
$tab1[] = $i++;
$tab1[] = $i++;
// routes - 3
print "tab1 a " . count($tab1) . " éléments\n";
$i = 0;
foreach ($tab1 as $élément) {
print "tab1[$i]=$élément\n";
$i++;
}
// delete last item
array_pop($tab1);
// routes - 4
print "tab1 a " . count($tab1) . " éléments\n";
for ($i = 0; $i < count($tab1); $i++)
print "tab1[$i]=$tab1[$i]\n";
// delete first element
array_shift($tab1);
// routes - 5
print "tab1 a " . count($tab1) . " éléments\n";
for ($i = 0; $i < count($tab1); $i++)
print "tab1[$i]=$tab1[$i]\n";
// addition at end of table
array_push($tab1, -2);
// routes - 6
print "tab1 a " . count($tab1) . " éléments\n";
for ($i = 0; $i < count($tab1); $i++)
print "tab1[$i]=$tab1[$i]\n";
// addition at the beginning of the table
array_unshift($tab1, -1);
// routes - 7
print "tab1 a " . count($tab1) . " éléments\n";
for ($i = 0; $i < count($tab1); $i++)
print "tab1[$i]=$tab1[$i]\n";
?>
The results:
Comments
The program above demonstrates operations for manipulating an array of values. There are two notations for arrays in PHP:
Array 1 is called an array, and array 2 is called a dictionary, where the elements are denoted as key => value. The notation $tableau=array("one",2,"three") refers to the value associated with the key "one". Here, that value is the string "two". Array 1 is simply a variant of the dictionary and could be written as:
We thus have $tableau[2]="three". Ultimately, there are only dictionaries. In the case of a classic array of n elements, the keys are the integers in the range [0,n-1].
- line 15: the function each($tableau) allows you to iterate over a dictionary. On each call, it returns a (key, value) pair from it.
- Line 14: The reset($dictionnaire) function sets the each function to the first (key, value) pair in the dictionary.
- Line 15: The while loop stops when the each function returns an empty pair at the end of the dictionary.
- line 19: the notation $tableau[]=value adds the element value as the last element of $tableau.
- line 25: the array is iterated over using a foreach loop. This syntax allows you to iterate over a dictionary—and thus an array—using two different syntaxes:
The first syntax returns a (key, value) pair for each iteration, while the second syntax returns only the value element of the dictionary.
- Line 31: The function array_pop($tableau) removes the last element from $tableau
- line 39: the function array_shift($tableau) removes the first element from $tableau
- line 47: the function array_push($tableau,value) adds value as the last element of $tableau
- line 39: the function array_unshift($tableau,value) adds value as the first element of $tableau
3.3.2. The dictionary (exemple_05)
<?php
// dictionaries
$conjoints = array("Pierre" => "Gisèle", "Paul" => "Virginie", "Jacques" => "Lucette", "Jean" => "");
// routes - 1
print "Nombre d'éléments du dictionnaire : " . count($conjoints) . "\n";
reset($conjoints);
while (list($clé, $valeur) = each($conjoints))
print "conjoints[$clé]=$valeur\n";
// dictionary sorting on key
ksort($conjoints);
// routes - 2
reset($conjoints);
while (list($clé, $valeur) = each($conjoints))
print "conjoints[$clé]=$valeur\n";
// list of dictionary keys
$clés = array_keys($conjoints);
for ($i = 0; $i < count($clés); $i++)
print "clés[$i]=$clés[$i]\n";
// list of dictionary values
$valeurs = array_values($conjoints);
for ($i = 0; $i < count($valeurs); $i++)
print "valeurs[$i]=$valeurs[$i]\n";
// key search
existe($conjoints, "Jacques");
existe($conjoints, "Lucette");
existe($conjoints, "Jean");
// deleting a key-value
unset($conjoints["Jean"]);
print "Nombre d'éléments du dictionnaire : " . count($conjoints) . "\n";
foreach ($conjoints as $clé => $valeur) {
print "conjoints[$clé]=$valeur\n";
}
// end
exit;
function existe($conjoints, $mari) {
// checks whether the key $mari exists in the dictionary $conjoints
if (isset($conjoints[$mari]))
print "La clé [$mari] existe associée à la valeur [$conjoints[$mari]]\n";
else
print "La clé [$mari] n'existe pas\n";
}
?>
The results:
Comments
The code above applies to a dictionary what we previously saw for a simple array. We will only comment on the new features:
- line 13: the ksort (key sort) function allows you to sort a dictionary in the natural order of the keys.
- line 21: the function array_keys($dictionnaire) returns the list of dictionary keys as an array
- line 26: the function array_values($dictionnaire) returns the list of dictionary values as an array
- line 47: the function isset($variable) returns true if the variable $variable has been defined, false otherwise.
- line 36: the function unset($variable) deletes the variable $variable.
3.3.3. Multidimensional arrays (exemple_06)
<?php
// classic multidimensional tables
// initialization
$multi = array(array(0, 1, 2), array(10, 11, 12, 13), array(20, 21, 22, 23, 24));
// route
for ($i1 = 0; $i1 < count($multi); $i1++)
for ($i2 = 0; $i2 < count($multi[$i1]); $i2++)
print "multi[$i1][$i2]=" . $multi[$i1][$i2] . "\n";
// multidimensional dictionaries
// initialization
$multi = array("zéro" => array(0, 1, 2), "un" => array(10, 11, 12, 13), "deux" => array(20, 21, 22, 23, 24));
// route
foreach ($multi as $clé => $valeur)
for ($i2 = 0; $i2 < count($valeur); $i2++)
print "multi[$clé][$i2]=" . $multi[$clé][$i2] . "\n";
?>
Results:
Comments
- line 5: the elements of the array $multi are themselves arrays
- line 12: the array $multi becomes a dictionary (key, value) where each value is an array
3.3.4. Links between strings and arrays (exemple_07)
<?php
// string to array
$chaine = "1:2:3:4";
$tab = explode(":", $chaine);
// parcours tableau
print "tab a " . count($tab) . " éléments\n";
for ($i = 0; $i < count($tab); $i++)
print "tab[$i]=$tab[$i]\n";
// table to string
$chaine2 = implode(":", $tab);
print "chaine2=$chaine2\n";
// add an empty field
$chaine.=":";
print "chaîne=$chaine\n";
$tab = explode(":", $chaine);
// parcours tableau
print "tab a " . count($tab) . " éléments\n";
for ($i = 0; $i < count($tab); $i++)
print "tab[$i]=$tab[$i]\n"; // we now have 5 elements, the last being empty
// let's add another empty field
$chaine.=":";
print "chaîne=$chaine\n";
$tab = explode(":", $chaine);
// parcours tableau
print "tab a " . count($tab) . " éléments\n";
for ($i = 0; $i < count($tab); $i++)
print "tab[$i]=$tab[$i]\n"; // we now have 6 elements, the last two being empty
?>
Results:
Comments
- Line 5: The explode($séparateur,$chaine) function retrieves the fields from $chaine separated by $séparateur. Thus, explode(":",$chaine) retrieves the elements of $chaine separated by the string ":" in the form of an array.
- The implode($séparateur,$tableau) function performs the inverse operation of the explode function. It returns a string consisting of the elements of $tableau separated by $séparateur.
3.4. Strings
3.4.1. Syntax (exemple_08)
<?php
// string notation
$chaine1 = "un";
$chaine2 = 'un';
print "[$chaine1,$chaine2]\n";
?>
Results:
3.4.2. Comparison (exemple_09)
<?php
// string comparison tests
compare("abcd", "abcd");
compare("", "");
compare("1", "");
exit;
function compare($chaine1, $chaine2) {
// compare string1 and string2
if ($chaine1 == $chaine2)
print "[$chaine1] est égal à [$chaine2]\n";
else
print "[$chaine1] est différent de [$chaine2]\n";
}
?>
Results:
3.5. Regular expressions (exemple_10)
<?php
// regular expression in PHP
// retrieve the various fields of a string
// the model: a sequence of numbers surrounded by any characters
// you only want to retrieve the sequence of digits
$modèle = "/(\d+)/";
// the chain is compared with the model
compare($modèle, "xyz1234abcd");
compare($modèle, "12 34");
compare($modèle, "abcd");
// the model: a sequence of numbers surrounded by any characters
// we want the sequence of digits and the fields that follow and precede them
$modèle = "/^(.*?)(\d+)(.*?)$/";
// the chain is compared with the model
compare($modèle, "xyz1234abcd");
compare($modèle, "12 34");
compare($modèle, "abcd");
// the template - a date in dd/mm/aa format
$modèle = "/^\s*(\d\d)\/(\d\d)\/(\d\d)\s*$/";
compare($modèle, "10/05/97");
compare($modèle, " 04/04/01 ");
compare($modèle, "5/1/01");
// the model - a decimal number
$modèle = "/^\s*([+|-]?)\s*(\d+\.\d*|\.\d+|\d+)\s*/";
compare($modèle, "187.8");
compare($modèle, "-0.6");
compare($modèle, "4");
compare($modèle, ".6");
compare($modèle, "4.");
compare($modèle, " + 4");
// end
exit;
// --------------------------------------------------------------------------
function compare($modèle, $chaîne) {
// compares the $chaîne string with the $modèle model
// the chain is compared with the model
$correspond = preg_match($modèle, $chaîne, $champs);
// displaying results
print "\nRésultats($modèle,$chaîne)\n";
if ($correspond) {
for ($i = 0; $i < count($champs); $i++)
print "champs[$i]=$champs[$i]\n";
} else
print "La chaîne [$chaîne] ne correspond pas au modèle [$modèle]\n";
}
?>
Results:
Comments
- lines 3-4: Here we use regular expressions to extract the various fields from a string. Regular expressions allow us to overcome the limitations of the implode function. The principle is to compare a string to another string called a pattern using the preg_match function:
$correspond = preg_match($modèle, $chaîne, $champs);
The preg_match function returns a Boolean value indicating whether the pattern can be found in the string. If so, $champs[0] represents the substring matching the pattern. Furthermore, if pattern contains subpatterns in parentheses, $champs[1] is the part of $chaîne corresponding to the first subpattern, $champs[2] is the part of $chaîne corresponding to the second subpattern, and so on...
Let’s consider the first example. The pattern is defined on line 8: it matches a sequence of one or more (+) digits (\d) located anywhere in a string. Additionally, the pattern defines a subpattern enclosed in parentheses.
- Line 10: the pattern /(\d+)/ (a sequence of one or more digits anywhere in the string) is compared to the string "xyz1234abcd". We see that the substring 1234 matches the pattern. We therefore have $champs[0] equal to "1234". Additionally, the pattern has subpatterns in parentheses. We will have $champs[1] = "1234".
- Line 11: The pattern /(\d+)/ is compared to the string "12 34". We see that the substrings 12 and 34 match the pattern. The comparison stops at the first substring that matches the pattern. We therefore have $champs[0]=12 and $champs[1]=12.
- Line 12: The pattern /(\d+)/ is compared to the string "abcd". No match is found.
Let's explain the patterns used in the rest of the code:
$modèle = "/^(.*?)(\d+)(.*?)$/";
start of string (^), then 0 or more (*) arbitrary characters (.), then 1 or more (+) digits, then again 0 or more (*) arbitrary characters (.). The pattern (.*) matches zero or more arbitrary characters. Such a pattern will match any string. Thus, the pattern /^(.*)(\d+)(.*)$/ will never be found because the first subpattern (.*) will consume the entire string. The pattern (.*?)(\d+) matches 0 or more any characters up to the next subpattern (?), in this case \d+. Therefore, the digits are no longer matched by the (.*). The pattern above thus matches [début de chaîne (^), une suite de caractères quelconques (.*?), une suite d'un ou plusieurs chiffres (\d+), une suite de caractères quelconques (.*?), la fin de la chaîne ($)].
$modèle = "/^\s*(\d\d)\/(\d\d)\/(\d\d)\s*$/";
matches [début de chaîne (^), 2 chiffres (\d\d), le caractère / (\/), 2 chiffres, /, 2 chiffres, une suite de 0 ou plusieurs espaces (\s*), la fin de chaîne ($)]
$modèle = "/^\s*([+|-]?)\s*(\d+\.\d*|\.\d+|\d+)\s*/";
[début de chaîne (^), 0 ou plusieurs espaces (\s*), un signe + ou - [+|-] présent 0 ou 1 fois (?), une suite de 0 ou plusieurs espaces (\s*), 1 ou plusieurs chiffres suivis d'un point décimal suivi de zéro ou plusieurs chiffres (\d+\.\d*) ou (|) un point décimal (\.) suivi d'un ou plusieurs chiffres (\d+) ou (|) un ou plusieurs chiffres (\d+), une suite de 0 ou plusieurs espaces (\s*)].
3.6. How to Pass Parameters to Functions (exemple_11)
<?php
// parameter step mode
function f(&$i, $j) {
// $i will be obtained by reference
// $j will be obtained by value
$i++;
$j++;
print "f[i,j]=[$i,$j]\n";
}
// ----------------------------------------tests
$i = 0;
$j = 0;
// $i and $j passed by values
f($i, $j);
print "test[i,j]=[$i,$j]\n";
?>
Results:
Comments
The code above demonstrates the two ways of passing parameters to a function. Consider the following example:
- line 1: defines the formal parameters $a and $b of the function f. This function processes these two formal parameters and returns a result.
- line 7: calls the function f with two actual parameters $i and $j. The relationships between the formal parameters ($a, $b) and the actual parameters ($i, $j) are defined by line 7
- &$a: the & sign indicates that the formal parameter $a will take on the value of the address of the actual parameter $i. In other words, $a and $i are two references to the same memory location. Manipulating the formal parameter $a is equivalent to manipulating the actual parameter $i. This is demonstrated by the code execution. This passing mode is suitable for output parameters and large data sets such as arrays and dictionaries. This passing mode is called pass-by-reference.
- $b: The formal parameter $b will take on the value of the actual parameter $j. This is a pass-by-value. The formal and actual parameters are two different variables. Manipulating the formal parameter $b has no effect on the actual parameter $j. This is demonstrated by the code execution. This passing mode is suitable for input parameters.
- Consider the exchange function that accepts two formal parameters, $a and $b. The function swaps the values of these two parameters. Thus, during a call to
exchange*($i, $j)*, the calling code expects the values of the two actual parameters to be swapped. These are therefore output parameters (they are modified). We will therefore write:
3.7. Results returned by a function (exemple_12)
<?php
// results rendered by a function
// a function can return several values in an array
list($res1, $res2, $res3) = f1(10);
print "[$res1,$res2,$res3]\n";
$res = f1(10);
for ($i = 0; $i < count($res); $i++)
print "res[$i]=$res[$i]\n";
// a function can render a pseudo-object
$res = f2(10);
print "[$res->res1,$res->res2,$res->res3]\n";
// end
exit;
// function f1
function f1($valeur) {
// returns an array ($valeur+1,$aleur+2,$baleur+3)
return array($valeur + 1, $valeur + 2, $valeur + 3);
}
// function f2
function f2($valeur) {
// renders a pseudo-object ($valeur+1,$aleur+2,$baleur+3)
$res->res1 = $valeur + 1;
;
$res->res2 = $valeur + 2;
$res->res3 = $valeur + 3;
// makes the object
return $res;
}
?>
Results
Comments
- The previous program shows that a function PHP can return a set of results rather than a single one, in the form of an array or an object. The concept of an object is explained in more detail below
- lines 19–22: the function f1 returns multiple values in the form of an array
- lines 25–33: the function f2 returns multiple values in the form of an object
3.8. Text files (exemple_13)
<?php
// sequential operation of a text file
// this is a set of lines of the form login:pwd:uid:gid:infos:dir:shell
// each line is put into a dictionary in the form login => uid:gid:infos:dir:shell
// set the file name
$INFOS = "infos.txt";
// we open it in creation
if (!$fic = fopen($INFOS, "w")) {
print "Erreur d'ouverture du fichier $INFOS en écriture\n";
exit;
}
// generate arbitrary content
for ($i = 0; $i < 100; $i++)
fputs($fic, "login$i:pwd$i:uid$i:gid$i:infos$i:dir$i:shell$i\n");
// close the file
fclose($fic);
// we use it - fgets keeps the end-of-line marker
// this prevents the retrieval of an empty string when reading a blank line
// we open it in creation
if (!$fic = fopen($INFOS, "r")) {
print "Erreur d'ouverture du fichier $INFOS en lecture\n";
exit;
}
while ($ligne = fgets($fic, 1000)) {
// delete the end-of-line marker if it exists
$ligne = cutNewLineChar($ligne);
// put the line in a table
$infos = explode(":", $ligne);
// retrieve login
$login = array_shift($infos);
// we neglect the pwd
array_shift($infos);
// create a dictionary entry
$dico[$login] = $infos;
}
// close it
fclose($fic);
// using the dictionary
afficheInfos($dico, "login10");
afficheInfos($dico, "X");
// end
exit;
// --------------------------------------------------------------------------
function afficheInfos($dico, $clé) {
// displays the value associated with key in the $dico dictionary if it exists
if (isset($dico[$clé])) {
// value exists - is it a painting?
$valeur = $dico[$clé];
if (is_array($valeur)) {
print "[$clé," . join(":", $valeur) . "]\n";
} else {
// $valeur is not an array
print "[$clé,$valeur]\n";
}
} else {
// $clé is not a key in the $dico dictionary
print "la clé [$clé] n'existe pas\n";
}
}
// --------------------------------------------------------------------------
function cutNewLinechar($ligne) {
// delete the end-of-line mark from $ligne if it exists
$L = strlen($ligne); // line length
while (substr($ligne, $L - 1, 1) == "\n" or substr($ligne, $L - 1, 1) == "\r") {
$ligne = substr($ligne, 0, $L - 1);
$L--;
}
// end
return($ligne);
}
?>
The file infos.txt:
login0:pwd0:uid0:gid0:infos0:dir0:shell0
login1:pwd1:uid1:gid1:infos1:dir1:shell1
login2:pwd2:uid2:gid2:infos2:dir2:shell2
...
login98:pwd98:uid98:gid98:infos98:dir98:shell98
login99:pwd99:uid99:gid99:infos99:dir99:shell99
The results:
Comments
- line 11: fopen(nom_fichier,"w") opens the file nom_fichier for writing (w=write). If the file does not exist, it is created. If it exists, it is cleared. If creation fails, fopen returns false. In the statement if (!$fic = fopen($INFOS, "w")) {…}, there are two successive operations: 1) $fic = fopen(..) 2) if( ! $fic) {...}
- line 18: fputs($fic,$chaîne) writes a string to the file $fic.
- line 20: fclose($fic) closes the file $fic.
- Line 25: fopen(nom_fichier,"r") opens the file nom_fichier for reading (r=read). If opening fails (the file does not exist), fopen returns false.
- Line 29: fgets($fic, 1000) reads the next line of the file, limited to 1000 characters. In the while loop ($ligne = fgets($fic, 1000)) {…}, there are two successive operations: 1) $ligne = fgets(...) 2) while ( ! $ligne). After the last character of the file has been read, the fgets function returns an empty string and the while loop stops.
- line 31: the fgets function reads a line of text, including the newline character. The cutNewLineChar function in lines 70–79 removes any newline character.
- Line 72: The strlen($chaîne) function returns the number of characters in $chaîne.
- Line 73: The function substr($ligne, $position, $taille) returns $taille characters from $ligne, starting from character #$position, where the first character is #0. On Windows machines, the end-of-line marker is "\r\n". On Unix machines, it is the string "\n".
- Line 35: The function array_shift($tableau) removes the first element of $tableau and returns it as the result.
- Line 37: The function array_shift($tableau) is called, but its result is ignored
- line 57: the function is_array($variable) returns true if $variable is an array, false otherwise.
- line 58: the join function does the same thing as the implode function we encountered earlier