Skip to content

3. The Basics of PHP

3.1. A first example

Below is a program demonstrating the basic features of PHP.

3.1.1. The program (example_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";
}
 
// a while loop
$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];   //$s//$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:

nom=dupont
tableau[0]=un
tableau[1]=deux
tableau[2]=3
tableau[3]=4
[chaine1,chaine2,chaine1chaine2]
chaine=chaine1
type[4]=integer
type[chaine1]=string
type[Array]=array
type[a changé]=string
res1=14
(res1,res2,res3)=[un,0,100]
t[0]=un
t[1]=0
t[2]=100
t[0]=un
t[2]=100
t[0]=8
t[1]=5
somme=13

Comments

  • Line 5: In PHP, variable types are not declared. Variables have a dynamic type that can change over time
  • $name represents the variable with the identifier name
  • Line 8: To display text on 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 $name[$i] represents the $ith element of the $array array.
  • line 14: the count($tableau) function returns the number of elements in the $tableau array
  • line 18: strings are enclosed in double quotes " or single quotes '. Inside double quotes, variables $variable are evaluated, but not inside single quotes.
  • line 21: the list function allows you to group variables into a list and assign a value to them with a single assignment operation. Here, $string1="string1" and $string2="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 (example_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:

1
2
3
4
f1[i,j]=[1,10]
f2[i,j]=[2,20]
f3[i,j]=[4,30]
test[i,j]=[2,0]

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. In line 7, the notation $GLOBALS["i"] refers to the global variable $i of the main program. If we write
$i=$GLOBALS["i"]

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

$i=&Globals["i"]

causes the local variable $i to have the same memory address as the global variable $i. Manipulating the local variable $i is then equivalent to manipulating the global variable $i.

3.2.2. Program 2 (example_03)


<?php
 
// the scope of a variable is global to code blocks
$i = 0; {
  $i = 4;
  $i++;
}
print "i=$i\n";
?>

The results:

i=5

Comments

In some languages, a variable defined within curly braces has a scope limited to those braces: it is not accessible 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 (example_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:

tab1 a 6 éléments
tab1[0]=0
tab1[1]=1
tab1[2]=2
tab1[3]=3
tab1[4]=4
tab1[5]=5
tab1 a 6 éléments
tab1[0]=0
tab1[1]=1
tab1[2]=2
tab1[3]=3
tab1[4]=4
tab1[5]=5
tab1 a 8 éléments
tab1[0]=0
tab1[1]=1
tab1[2]=2
tab1[3]=3
tab1[4]=4
tab1[5]=5
tab1[6]=6
tab1[7]=7
tab1 a 7 éléments
tab1[0]=0
tab1[1]=1
tab1[2]=2
tab1[3]=3
tab1[4]=4
tab1[5]=5
tab1[6]=6
tab1 a 6 éléments
tab1[0]=1
tab1[1]=2
tab1[2]=3
tab1[3]=4
tab1[4]=5
tab1[5]=6
tab1 a 7 éléments
tab1[0]=1
tab1[1]=2
tab1[2]=3
tab1[3]=4
tab1[4]=5
tab1[5]=6
tab1[6]=-2
tab1 a 8 éléments
tab1[0]=-1
tab1[1]=1
tab1[2]=2
tab1[3]=3
tab1[4]=4
tab1[5]=5
tab1[6]=6
tab1[7]=-2

Comments

The program above demonstrates operations for manipulating an array of values. There are two notations for arrays in PHP:

$tableau=array("un",2,"trois")
$contraires=array("petit"=>"grand", "beau"=>"laid", "cher"=>"bon marché")

Array 1 is called an array, and array 2 is called a dictionary, where elements are denoted as key => value. The notation $opposites["beautiful"] refers to the value associated with the key "beautiful." Here, that value is the string "ugly." Array 1 is simply a variant of the dictionary and could be written as:

$tableau=array(0=>"un",1=>2,2=>"trois")

Thus, we have $array[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 each($array) function allows you to iterate over a dictionary. On each call, it returns a (key, value) pair from it.
  • Line 14: The reset($dictionary) 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 $array[]=value adds the element value as the last element of $array.
  • line 25: the array is iterated over using a foreach. This syntax allows you to iterate over a dictionary, and therefore an array, using two syntaxes:
foreach($dictionnaire as $clé=>$valeur)
foreach($tableau as $valeur)

The first syntax returns a (key,value) pair on each iteration, while the second syntax returns only the value element of the dictionary.

  • Line 31: The array_pop($array) function removes the last element from $array
  • line 39: the array_shift($array) function removes the first element from $array
  • line 47: the array_push($array,value) function adds value as the last element of $array
  • line 39: the array_unshift($array,value) function adds value as the first element of $array

3.3.2. The dictionary (example_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:

Nombre d'éléments du dictionnaire : 4
conjoints[Pierre]=Gisèle
conjoints[Paul]=Virginie
conjoints[Jacques]=Lucette
conjoints[Jean]=
conjoints[Jacques]=Lucette
conjoints[Jean]=
conjoints[Paul]=Virginie
conjoints[Pierre]=Gisèle
clés[0]=Jacques
clés[1]=Jean
clés[2]=Paul
clés[3]=Pierre
valeurs[0]=Lucette
valeurs[1]=
valeurs[2]=Virginie
valeurs[3]=Gisèle
La clé [Jacques] existe associée à la valeur [Lucette]
La clé [Lucette] n'existe pas
La clé [Jean] existe associée à la valeur []
Nombre d'éléments du dictionnaire : 3
conjoints[Jacques]=Lucette
conjoints[Paul]=Virginie
conjoints[Pierre]=Gisèle

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 sorts a dictionary in the natural order of the keys.
  • line 21: the array_keys($dictionary) function returns the list of the dictionary’s keys as an array
  • line 26: the array_values($dictionary) function returns the list of the dictionary’s values as an array
  • line 47: the isset($variable) function returns true if the $variable has been defined, false otherwise.
  • Line 36: The unset($variable) function deletes the $variable.

3.3.3. Multidimensional arrays (example_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:

multi[0][0]=0
multi[0][1]=1
multi[0][2]=2
multi[1][0]=10
multi[1][1]=11
multi[1][2]=12
multi[1][3]=13
multi[2][0]=20
multi[2][1]=21
multi[2][2]=22
multi[2][3]=23
multi[2][4]=24
multi[zéro][0]=0
multi[zéro][1]=1
multi[zéro][2]=2
multi[un][0]=10
multi[un][1]=11
multi[un][2]=12
multi[un][3]=13
multi[deux][0]=20
multi[deux][1]=21
multi[deux][2]=22
multi[deux][3]=23
multi[deux][4]=24

Comments

  • line 5: the elements of the $multi array are themselves arrays
  • line 12: the $multi array becomes a dictionary (key, value) where each value is an array


<?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:

tab a 4 éléments
tab[0]=1
tab[1]=2
tab[2]=3
tab[3]=4
chaine2=1:2:3:4
chaîne=1:2:3:4:
tab a 5 éléments
tab[0]=1
tab[1]=2
tab[2]=3
tab[3]=4
tab[4]=
chaîne=1:2:3:4::
tab a 6 éléments
tab[0]=1
tab[1]=2
tab[2]=3
tab[3]=4
tab[4]=
tab[5]=

Comments

  • Line 5: The explode($separator,$string) function retrieves the fields of $string separated by $separator. Thus, explode(":",$string) returns the elements of $string separated by the string ":" as an array.
  • The implode($separator,$array) function performs the opposite operation of the explode function. It returns a string consisting of the elements of $array separated by $separator.

3.4. Strings

3.4.1. Syntax (example_08)


<?php
 
// string notation
$chaine1 = "un";
$chaine2 = 'un';
print "[$chaine1,$chaine2]\n";
?>

Results:

[un,un]

3.4.2. Comparison (example_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:

1
2
3
[abcd] est égal à [abcd]
[] est égal à []
[1] est différent de []

3.5. Regular expressions (example_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
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 numbers and the fields that follow and precede them
$modèle = "/^(.*?)(\d+)(.*?)$/";
// the chain is compared with the
compare($modèle, "xyz1234abcd");
compare($modèle, "12 34");
compare($modèle, "abcd");
 
// the template - a date in dd/mm/yy 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:

Résultats(/(\d+)/,xyz1234abcd)
champs[0]=1234
champs[1]=1234

Résultats(/(\d+)/,12 34)
champs[0]=12
champs[1]=12

Résultats(/(\d+)/,abcd)
La chaîne [abcd] ne correspond pas au modèle [/(\d+)/]

Résultats(/^(.*?)(\d+)(.*?)$/,xyz1234abcd)
champs[0]=xyz1234abcd
champs[1]=xyz
champs[2]=1234
champs[3]=abcd

Résultats(/^(.*?)(\d+)(.*?)$/,12 34)
champs[0]=12 34
champs[1]=
champs[2]=12
champs[3]= 34

Résultats(/^(.*?)(\d+)(.*?)$/,abcd)
La chaîne [abcd] ne correspond pas au modèle [/^(.*?)(\d+)(.*?)$/]

Résultats(/^\s*(\d\d)\/(\d\d)\/(\d\d)\s*$/,10/05/97)
champs[0]=10/05/97
champs[1]=10
champs[2]=05
champs[3]=97

Résultats(/^\s*(\d\d)\/(\d\d)\/(\d\d)\s*$/,  04/04/01  )
champs[0]=  04/04/01  
champs[1]=04
champs[2]=04
champs[3]=01

Résultats(/^\s*(\d\d)\/(\d\d)\/(\d\d)\s*$/,5/1/01)
La chaîne [5/1/01] ne correspond pas au modèle [/^\s*(\d\d)\/(\d\d)\/(\d\d)\s*$/]

Résultats(/^\s*([+|-]?)\s*(\d+\.\d*|\.\d+|\d+)\s*/,187.8)
champs[0]=187.8
champs[1]=
champs[2]=187.8

Résultats(/^\s*([+|-]?)\s*(\d+\.\d*|\.\d+|\d+)\s*/,-0.6)
champs[0]=-0.6
champs[1]=-
champs[2]=0.6

Résultats(/^\s*([+|-]?)\s*(\d+\.\d*|\.\d+|\d+)\s*/,4)
champs[0]=4
champs[1]=
champs[2]=4

Résultats(/^\s*([+|-]?)\s*(\d+\.\d*|\.\d+|\d+)\s*/,.6)
champs[0]=.6
champs[1]=
champs[2]=.6

Résultats(/^\s*([+|-]?)\s*(\d+\.\d*|\.\d+|\d+)\s*/,4.)
champs[0]=4.
champs[1]=
champs[2]=4.

Résultats(/^\s*([+|-]?)\s*(\d+\.\d*|\.\d+|\d+)\s*/, + 4)
champs[0]= + 4
champs[1]=+
champs[2]=4

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, $fields[0] represents the substring matching the pattern. Furthermore, if the pattern contains subpatterns enclosed in parentheses, $champs[1] is the portion of $string corresponding to the first subpattern, $champs[2] is the portion of $string corresponding to the second subpattern, and so on...

Consider the first example. The pattern is defined on line 8: it denotes a sequence of one or more (+) digits (\d) located anywhere in a string. Furthermore, 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. Therefore, $fields[0] will be equal to "1234". Furthermore, the pattern has subpatterns enclosed 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. Therefore, $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 (.*) denotes 0 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+) denotes 0 or more arbitrary characters up to the next subpattern (?), in this case \d+. Therefore, the digits are no longer captured by the pattern (.*). The pattern above thus matches [start of string (^), a sequence of any characters (.*?), a sequence of one or more digits (\d+), a sequence of any characters (.*?), end of string ($)].


$modèle = "/^\s*(\d\d)\/(\d\d)\/(\d\d)\s*$/";

matches [start of string (^), 2 digits (\d\d), the character / (\/), 2 digits, /, 2 digits, a sequence of 0 or more spaces (\s*), end of string ($)]


$modèle = "/^\s*([+|-]?)\s*(\d+\.\d*|\.\d+|\d+)\s*/";

[start of string (^), 0 or more spaces (\s*), a + or - sign [+|-] occurring 0 or 1 time (?), a sequence of 0 or more spaces (\s*), 1 or more digits followed by a decimal point followed by zero or more digits (\d+\.\d*) or (|) a decimal point (\.) followed by one or more digits (\d+) or (|) one or more digits (\d+), a sequence of 0 or more spaces (\s*)].

3.6. Function parameter passing mode (example_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:

f[i,j]=[1,1]
test[i,j]=[1,0]

Comments

The code above demonstrates the two ways of passing parameters to a function. Let’s consider the following example:

1
2
3
4
5
6
7
function f(&$a,$b){

}

// main program
$i=10; $j=20;
f($i,$j);
  • line 1: defines the formal parameters $a and $b of function f. This function manipulates these two formal parameters and returns a result.
  • line 7: calls the function f with two actual parameters $i and $j. The bindings between the formal parameters ($a, $b) and the actual parameters ($i, $j) are defined by line 7
  • &$a: the & symbol 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 method is suitable for output parameters and large data sets such as arrays and dictionaries. This passing method is called pass-by-reference.
  • $b: The formal parameter $b will take on the value of the actual parameter $j. This is passing 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 swap function, which takes two formal parameters $a and $b. The function swaps the values of these two parameters. Thus, during a call to swap($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:
function échange(&$a,&$b){....}

3.7. Results returned by a function (example_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

1
2
3
4
5
[11,12,13]
res[0]=11
res[1]=12
res[2]=13
[11,12,13]

Comments

  • The previous program shows that a PHP function 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 (example_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 infos.txt file:

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:

[login10,uid10:gid10:infos10:dir10:shell10]
la clé [X] n'existe pas

Comments

  • Line 11: fopen(filename, "w") opens the file filename for writing (w = write). If the file does not exist, it is created. If it exists, its contents are 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,$string) writes string to the $fic file.
  • Line 20: fclose($fic) closes the $fic file.
  • line 25: fopen(filename, "r") opens the filename file for reading (r=read). If opening fails (the file does not exist), fopen returns a value of false.
  • line 29: fgets($fic,1000) reads the next line of the file, limited to 1000 characters. In the operation while ($line = fgets($fic, 1000)) {…}, there are two successive operations: 1) $line = fgets(...) 2) while ( ! $line). 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($string) function returns the number of characters in $string.
  • Line 73: The substr($line, $position, $size) function returns $size characters from $line, 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 array_shift($array) function removes the first element from $array and returns it as the result.
  • line 37: the array_shift($array) function is used, but its result is ignored
  • Line 57: The is_array($variable) function 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