cvs: phpdoc-fr /language control-structures.xml
| From: | Damien Seguy | Date: | Wed, 11 Sep 2002 02:17:21 +0000 |
| Subject: | cvs: phpdoc-fr /language control-structures.xml | ||
| Groups: | php.doc.fr | ||
| Request: | Send a blank email to doc-fr+get-46@lists.php.net to get a copy of this message | ||
dams Tue Sep 10 22:17:21 2002 EDT
Modified files:
/phpdoc-fr/language control-structures.xml
Log:
examples to cdata
Index: phpdoc-fr/language/control-structures.xml diff -u phpdoc-fr/language/control-structures.xml:1.17 phpdoc-fr/language/control-structures.xml:1.18 --- phpdoc-fr/language/control-structures.xml:1.17 Wed Dec 12 15:50:06 2001 +++ phpdoc-fr/language/control-structures.xml Tue Sep 10 22:17:20 2002 @@ -1,5 +1,5 @@ <?xml version="1.0" encoding="iso-8859-1"?> -<!-- $Revision: 1.17 $ --> +<!-- $Revision: 1.18 $ --> <chapter id="control-structures"> <title>Les structures de contrôle</title> <simpara> @@ -23,10 +23,12 @@ sont les mêmes en PHP qu'en C : <informalexample> <programlisting role="php"> -<?php + <![CDATA[ +<?php if (expression) commandes -?> +?> +]]> </programlisting> </informalexample> </para> @@ -43,10 +45,12 @@ que <replaceable>$b</replaceable>: <informalexample> <programlisting role="php"> -<?php - if ($a > $b) + <![CDATA[ +<?php + if ($a > $b) print "a est plus grand que b"; -?> +?> +]]> </programlisting> </informalexample> </para> @@ -62,12 +66,14 @@ <replaceable>$b</replaceable>: <informalexample> <programlisting role="php"> -<?php -if ($a > $b) { + <![CDATA[ +<?php +if ($a > $b) { print "a est plus grand que b"; $b = $a; } -?> +?> +]]> </programlisting> </informalexample> </para> @@ -94,13 +100,15 @@ sinon: <informalexample> <programlisting role="php"> -<?php -if ($a > $b) { + <![CDATA[ +<?php +if ($a > $b) { print "a est plus grand que b"; } else { print "a est plus petit que b"; } -?> +?> +]]> </programlisting> </informalexample> Les instructions après le <literal>else</literal> ne sont @@ -126,15 +134,17 @@ <computeroutput>a est plus petit que b</computeroutput>: <informalexample> <programlisting role="php"> -<?php -if ($a > $b) { + <![CDATA[ +<?php +if ($a > $b) { print "a est plus grand que b"; } elseif ($a == $b) { print "a est égal à b"; } else { print "a est plus petit que b"; } -?> +?> +]]> </programlisting> </informalexample> </para> @@ -170,9 +180,15 @@ <literal>endfor;</literal>, ou <literal>endswitch;</literal>. <informalexample> <programlisting role="php"> -<?php if ($a == 5): ?> - A vaut 5 -<?php endif; ?> + <![CDATA[ +<?php + if ($a == 5): +?> + A vaut 5 +<?php + endif; + ?> +]]> </programlisting> </informalexample> </para> @@ -189,7 +205,8 @@ <literal>else</literal> utilisant cette autre syntaxe: <informalexample> <programlisting role="php"> -<?php + <![CDATA[ +<?php if ($a == 5): print "a égale 5"; print "..."; @@ -199,7 +216,8 @@ else: print "a ne vaut ni 5 ni 6"; endif; -?> +?> +]]> </programlisting> </informalexample> </para> @@ -219,9 +237,11 @@ est le suivant : <informalexample> <programlisting role="php"> -<?php -while (expression) commandes -?> + <![CDATA[ +<?php + while (expression) commandes +?> +]]> </programlisting> </informalexample> </para> @@ -247,9 +267,11 @@ de parenthèses ou en utilisant la syntaxe suivante: <informalexample> <programlisting role="php"> -<?php + <![CDATA[ +<?php while (expression): commandes ... endwhile; -?> +?> +]]> </programlisting> </informalexample> </para> @@ -258,20 +280,22 @@ de 1 à 10: <informalexample> <programlisting role="php"> -<?php + <![CDATA[ +<?php /* exemple 1 */ $i = 1; -while ($i <= 10) { +while ($i <= 10) { print $i++; /* La valeur affiche est $i avant l'incrémentation (post-incrémentation) */ } /* exemple 2 */ $i = 1; -while ($i <= 10): +while ($i <= 10): print $i; $i++; endwhile; -?> +?> +]]> </programlisting> </informalexample> </para> @@ -295,12 +319,14 @@ Il n'y a qu'une syntaxe possible pour les boucles <literal>do..while</literal>: <informalexample> <programlisting role="php"> -<?php + <![CDATA[ +<?php $i = 0; do { print $i; } while ($i>0); -?> +?> +]]> </programlisting> </informalexample> </para> @@ -321,20 +347,22 @@ Le code suivant montre une utilisation possible: <informalexample> <programlisting role="php"> -<?php + <![CDATA[ +<?php do { - if ($i < 5) { + if ($i < 5) { print "i n'est pas suffisamment grand"; break; } $i *= $factor; - if ($i < $minimum_limit) { + if ($i < $minimum_limit) { break; } print "i est bon"; ...process i... } while(0); -?> +?> +]]> </programlisting> </informalexample> </para> @@ -353,9 +381,11 @@ La syntaxe des boucles for est la suivante: <informalexample> <programlisting role="php"> -<?php -for (expr1; expr2; expr3) statement -?> + <![CDATA[ +<?php + for (expr1; expr2; expr3) statement +?> +]]> </programlisting> </informalexample> </para> @@ -391,14 +421,15 @@ 1 à 10: <informalexample> <programlisting role="php"> -<?php + <![CDATA[ +<?php /* exemple 1 */ -for ($i = 1; $i <= 10; $i++) { +for ($i = 1; $i <= 10; $i++) { print $i; } /* exemple 2 */ for ($i = 1;;$i++) { - if ($i > 10) { + if ($i > 10) { break; } print $i; @@ -406,15 +437,16 @@ /* exemple 3 */ $i = 1; for (;;) { - if ($i > 10) { + if ($i > 10) { break; } print $i; $i++; } /* exemple 4 */ -for ($i = 1; $i <= 10; print $i, $i++) ; -?> +for ($i = 1; $i <= 10; print $i, $i++) ; +?> +]]> </programlisting> </informalexample> </para> @@ -429,9 +461,11 @@ <literal>for</literal> : <informalexample> <programlisting role="php"> -<?php -for (expr1; expr2; expr3): statement; ...; endfor; -?> + <![CDATA[ +<?php + for (expr1; expr2; expr3): statement; ...; endfor; +?> +]]> </programlisting> </informalexample> </para> @@ -455,10 +489,12 @@ une extension mineure mais pratique de la première: <informalexample> <programlisting role="php"> -<?php + <![CDATA[ +<?php foreach(array_expression as $value) commandes - foreach(array_expression as $key => $value) commandes -?> + foreach(array_expression as $key => $value) commandes +?> +]]> </programlisting> </informalexample> </para> @@ -505,29 +541,33 @@ manière identique : <informalexample> <programlisting role="php"> -<?php + <![CDATA[ +<?php reset($arr); while (list(, $value) = each ($arr)) { - echo "Valeur: $value<br>\n"; + echo "Valeur: $value<br>\n"; } foreach ($arr as $value) { - echo "Valeur: $value<br>\n"; + echo "Valeur: $value<br>\n"; } -?> +?> +]]> </programlisting> </informalexample> Les exemples suivants sont aussi fonctionnellement identiques : <informalexample> <programlisting role="php"> -<?php + <![CDATA[ +<?php reset($arr); while (list($key, $value) = each ($arr)) { - echo "Clé: $key; Valeur: $value<br>\n"; + echo "Clé: $key; Valeur: $value<br>\n"; } - foreach ($arr as $key => $value) { - echo "Clé: $key; Valeur: $value<br>\n"; + foreach ($arr as $key => $value) { + echo "Clé: $key; Valeur: $value<br>\n"; } -?> +?> +]]> </programlisting> </informalexample> </para> @@ -535,7 +575,8 @@ Voici quelques exemples de plus : <informalexample> <programlisting role="php"> -<?php + <![CDATA[ +<?php /* exemple 1: valeurs seules */ $a = array (1, 2, 3, 17); foreach ($a as $v) { @@ -545,17 +586,17 @@ $a = array (1, 2, 3, 17); $i = 0; /* pour l'illustration uniquement */ foreach($a as $v) { - print "\$a[$i] => $v.\n"; + print "\$a[$i] => $v.\n"; } /* exemple 3: clé et valeur */ $a = array ( - "un" => 1, - "deux" => 2, - "trois" => 3, - "dix-sept" => 17 + "un" => 1, + "deux" => 2, + "trois" => 3, + "dix-sept" => 17 ); -foreach($a as $k => $v) { - print "\$a[$k] => $v.\n"; +foreach($a as $k => $v) { + print "\$a[$k] => $v.\n"; } /* exemple 4: tableaux multi-dimensionnels */ $a[0][0] = "a"; @@ -571,7 +612,8 @@ foreach(array(1, 2, 3, 4, 5) as $v) { print "$v\n"; } -?> +?> +]]> </programlisting> </informalexample> </para> @@ -591,9 +633,10 @@ <para> <informalexample> <programlisting role="php"> -<?php + <![CDATA[ +<?php $i = 0; -while ($i < 10) { +while ($i < 10) { if ($arr[$i] == "stop") { break; /* Vous pouvez aussi écrire 'break 1;' ici. */ } @@ -604,16 +647,17 @@ while ( ++$i ) { switch ( $i ) { case 5: - echo "à 5<br>\n"; + echo "à 5<br>\n"; break 1; /* Ne sort que du switch. */ case 10: - echo "à 10; quitting<br>\n"; + echo "à 10; quitting<br>\n"; break 2; /* Sort du switch et du while. */ default: break; } } -?> +?> +]]> </programlisting> </informalexample> </para> @@ -634,7 +678,8 @@ <para> <informalexample> <programlisting role="php"> -<?php + <![CDATA[ +<?php while (list ($cle, $valeur) = each ($arr)) { if (!($cle % 2)) { // évite les membres impairs continue; @@ -642,19 +687,20 @@ fonction_quelconque($valeur); } $i = 0; -while ($i++ < 5) { - echo "Dehors<br>\n"; +while ($i++ < 5) { + echo "Dehors<br>\n"; while (1) { - echo " Milieu<br>\n"; + echo " Milieu<br>\n"; while (1) { - echo " Intérieur<br>\n"; + echo " Intérieur<br>\n"; continue 3; } - echo "Ceci n'est jamais atteint.<br>\n"; + echo "Ceci n'est jamais atteint.<br>\n"; } - echo "Ceci non plus.<br>\n"; + echo "Ceci non plus.<br>\n"; } -?> +?> +]]> </programlisting> </informalexample> </para> @@ -678,7 +724,8 @@ et l'autre en utilisant l'instruction <literal>switch</literal>: <informalexample> <programlisting role="php"> -<?php + <![CDATA[ +<?php if ($i == 0) { print "i égale 0"; } @@ -699,7 +746,8 @@ print "i égale 2"; break; } -?> +?> +]]> </programlisting> </informalexample> </para> @@ -720,7 +768,8 @@ toutes les instructions qui suivent. Par exemple : <informalexample> <programlisting role="php"> -<?php + <![CDATA[ +<?php switch ($i) { case 0: print "i égale 0"; @@ -729,7 +778,8 @@ case 2: print "i égale 2"; } -?> +?> +]]> </programlisting> </informalexample> </para> @@ -760,7 +810,8 @@ commandes du cas suivant. <informalexample> <programlisting role="php"> -<?php + <![CDATA[ +<?php switch ($i) { case 0: case 1: @@ -770,7 +821,8 @@ case 3: print "i égale 3"; } -?> +?> +]]> </programlisting> </informalexample> </para> @@ -781,7 +833,8 @@ être le dernier cas listé. Par exemple : <informalexample> <programlisting role="php"> -<?php + <![CDATA[ +<?php switch ($i) { case 0: print "i égale 0"; @@ -795,7 +848,8 @@ default: print "i n'est ni égal à 2, ni à 1, ni à 0."; } -?> +?> +]]> </programlisting> </informalexample> </para> @@ -812,7 +866,8 @@ <link linkend="control-structures.alternative-syntax">syntaxes alternatives</link>). <informalexample> <programlisting role="php"> -<?php + <![CDATA[ +<?php switch ($i): case 0: print "i égale 0"; @@ -826,7 +881,8 @@ default: print "i n'est ni égal à 2, ni à 1, ni à 0"; endswitch; -?> +?> +]]> </programlisting> </informalexample> </para> @@ -840,9 +896,11 @@ fonctions de contrôle : <informalexample> <programlisting> -<?php + <![CDATA[ +<?php declare (directive) statement -?> +?> +]]> </programlisting> </informalexample> </para> @@ -877,8 +935,9 @@ <para> <informalexample> <programlisting role="php"> -<PRE> -<?php +<![CDATA[ +<PRE> +<?php // Un fonction qui enregistre l'heure à laquelle elle est appelée function profile($dump = FALSE){ static $profile; @@ -896,21 +955,24 @@ profile(); // Exécute un bloc de code, et appele un tick toutes les deux secondes declare (ticks=2) { - for ($x = 1; $x < 50; ++$x) { - echo similar_text(md5($x), md5($x*$x)), "<br>"; + for ($x = 1; $x < 50; ++$x) { + echo similar_text(md5($x), md5($x*$x)), "<br>"; } } -?> +?> +]]> </programlisting> </informalexample> Pour voir le résultat : <informalexample> <programlisting role="php"> -<?php +<![CDATA[ +<?php // Affiche les données de la variable $profile print_r(profile(TRUE)); -?> -</pre> +?> +</pre> +]]> </programlisting> </informalexample> Cet exemple profile le code PHP dans le bloc de déclaration, @@ -981,9 +1043,11 @@ Pour cela, il faut utiliser <function>include</function>. <informalexample> <programlisting role="php"> -<?php +<![CDATA[ +<?php require('header.inc'); -?> +?> +]]> </programlisting> </informalexample> </para> @@ -996,14 +1060,16 @@ et vice-versa. <informalexample> <programlisting role="php"> -<?php +<![CDATA[ +<?php require ("file.inc?varone=1&vartwo=2"); /* Ne fonctionne pas. */ $varone = 1; $vartwo = 2; require ("file.inc"); /* $varone et $vartwo seront accessibles à file.inc */ -?> +?> +]]> </programlisting> </informalexample> </para> @@ -1048,12 +1114,14 @@ fois un fichier, ou même des fichiers différents. <informalexample> <programlisting role="php"> -<?php +<![CDATA[ +<?php $files = array ('premier.inc', 'second.inc', 'troisieme.inc'); -for ($i = 0; $i < count($files); $i++) { +for ($i = 0; $i < count($files); $i++) { include $files[$i]; } -?> +?> +]]> </programlisting> </informalexample> </para> @@ -1075,7 +1143,9 @@ bloc si elle est incluse dans une structure conditionnelle. <informalexample> <programlisting role="php"> -<?php +<![CDATA[ +<?php + /* Ceci est faux, et ne fonctionnera pas comme on l'attend. */ if ($condition) include($file); @@ -1087,7 +1157,8 @@ } else { include($other); } -?> +?> +]]> </programlisting> </informalexample> </para> @@ -1110,22 +1181,26 @@ et est placé dans le même dossier que le fichier principal : <programlisting role="php"> -<?php -echo "Avant le retour<br>\n"; +<![CDATA[ +<?php +echo "Avant le retour<br>\n"; if (1) { return 27; } -echo "Après le retour <br>\n"; -?> +echo "Après le retour <br>\n"; +?> +]]> </programlisting> </para> <para> On suppose que le fichier <filename>main.html</filename> contient ceci : <programlisting role="php"> -<?php +<![CDATA[ +<?php $retval = include ('test.inc'); -echo "Fichier inclus: '$retval'<br>\n"; -?> +echo "Fichier inclus: '$retval'<br>\n"; +?> +]]> </programlisting> </para> <para> @@ -1144,10 +1219,12 @@ été modifié et contient maintenant le code suivant : <programlisting role="php"> -<?php -include ('test.inc'); -echo "Retour dans le main.html<br>\n"; -?> +<![CDATA[ +<?php + include ('test.inc'); + echo "Retour dans le main.html<br>\n"; +?> +]]> </programlisting> </para> <para> @@ -1187,12 +1264,15 @@ <link linkend="language.basic-syntax.phpmode">les balises habituelles de PHP</link>. <informalexample> <programlisting role="php"> -<?php -include ("file.inc?varone=1&vartwo=2"); /* ne fonctionne pas. */ -$varone = 1; -$vartwo = 2; -include ("file.inc"); /* $varone et $vartwo sont accessibles dans file.inc */ -?> +<![CDATA[ +<?php + include ("file.inc?varone=1&vartwo=2"); /* ne fonctionne pas. */ + $varone = 1; + $vartwo = 2; + include ("file.inc"); + /* $varone et $vartwo sont accessibles dans file.inc */ +?> +]]> </programlisting> </informalexample> </para> @@ -1227,19 +1307,22 @@ <example> <title>utils.inc</title> <programlisting role="php"> -<?php +<![CDATA[ +<?php define(PHPVERSION, floor(phpversion())); echo "LES GLOBALES SONT SYMPAS\n"; function goodTea() { return "Le Earl Grey est délicieux!"; } -?> +?> +]]> </programlisting> </example> <example> <title>foolib.inc</title> <programlisting role="php"> -<?php +<![CDATA[ +<?php require ("utils.inc"); function showVar($var) { if (PHPVERSION == 4) { @@ -1249,24 +1332,27 @@ } } // Une série de fonctions -?> +?> +]]> </programlisting> </example> Puis, vous écrivez un script <literal>cause_error_require.php</literal> <example> <title>cause_error_require.php</title> <programlisting role="php"> -<?php -require("foolib.inc"); +<![CDATA[ +<?php + require("foolib.inc"); /* Ceci génère une erreur*/ -require("utils.inc"); -$foo = array("1",array("complex","quaternion")); -echo "Ce code requiert utils.inc une deuxième fois, car il est requis \n"; -echo "dans foolib.inc\n"; -echo "Utilisation de GoodTea: ".goodTea()."\n"; -echo "Affichage de foo: \n"; -showVar($foo); -?> + require("utils.inc"); + $foo = array("1",array("complex","quaternion")); + echo "Ce code requiert utils.inc une deuxième fois, car il est requis \n"; + echo "dans foolib.inc\n"; + echo "Utilisation de GoodTea: ".goodTea()."\n"; + echo "Affichage de foo: \n"; + showvar($foo); +?> +]]> </programlisting> </example> Lorsque vous exécutez le script ci-dessus, le @@ -1286,20 +1372,24 @@ <example> <title>foolib.inc (corrigé)</title> <programlisting role="php"> -<?php -require_once("utils.inc"); -function showVar($var) { -?> +<![CDATA[ +<?php + require_once("utils.inc"); + function showVar($var) { +?> +]]> </programlisting> </example> <example> <title>avoid_error_require_once.php</title> <programlisting role="php"> -<?php -require_once("foolib.inc"); -require_once("utils.inc"); -$foo = array("1",array("complexe","quaternion")); -?> +<![CDATA[ +<?php + require_once("foolib.inc"); + require_once("utils.inc"); + $foo = array("1",array("complexe","quaternion")); +?> +]]> </programlisting> </example> L'exécution de ce script, sous PHP 4.0.1pl2, donne : @@ -1312,11 +1402,11 @@ Affichage de foo: Array ( - [0] => 1 - [1] => Array + [0] => 1 + [1] => Array ( - [0] => complexe - [1] => quaternion + [0] => complexe + [1] => quaternion ) ) </programlisting>
Index: phpdoc-fr/language/control-structures.xml diff -u phpdoc-fr/language/control-structures.xml:1.17 phpdoc-fr/language/control-structures.xml:1.18 --- phpdoc-fr/language/control-structures.xml:1.17 Wed Dec 12 15:50:06 2001 +++ phpdoc-fr/language/control-structures.xml Tue Sep 10 22:17:20 2002 @@ -1,5 +1,5 @@ <?xml version="1.0" encoding="iso-8859-1"?> -<!-- $Revision: 1.17 $ --> +<!-- $Revision: 1.18 $ --> <chapter id="control-structures"> <title>Les structures de contrôle</title> <simpara> @@ -23,10 +23,12 @@ sont les mêmes en PHP qu'en C : <informalexample> <programlisting role="php"> -<?php + <![CDATA[ +<?php if (expression) commandes -?> +?> +]]> </programlisting> </informalexample> </para> @@ -43,10 +45,12 @@ que <replaceable>$b</replaceable>: <informalexample> <programlisting role="php"> -<?php - if ($a > $b) + <![CDATA[ +<?php + if ($a > $b) print "a est plus grand que b"; -?> +?> +]]> </programlisting> </informalexample> </para> @@ -62,12 +66,14 @@ <replaceable>$b</replaceable>: <informalexample> <programlisting role="php"> -<?php -if ($a > $b) { + <![CDATA[ +<?php +if ($a > $b) { print "a est plus grand que b"; $b = $a; } -?> +?> +]]> </programlisting> </informalexample> </para> @@ -94,13 +100,15 @@ sinon: <informalexample> <programlisting role="php"> -<?php -if ($a > $b) { + <![CDATA[ +<?php +if ($a > $b) { print "a est plus grand que b"; } else { print "a est plus petit que b"; } -?> +?> +]]> </programlisting> </informalexample> Les instructions après le <literal>else</literal> ne sont @@ -126,15 +134,17 @@ <computeroutput>a est plus petit que b</computeroutput>: <informalexample> <programlisting role="php"> -<?php -if ($a > $b) { + <![CDATA[ +<?php +if ($a > $b) { print "a est plus grand que b"; } elseif ($a == $b) { print "a est égal à b"; } else { print "a est plus petit que b"; } -?> +?> +]]> </programlisting> </informalexample> </para> @@ -170,9 +180,15 @@ <literal>endfor;</literal>, ou <literal>endswitch;</literal>. <informalexample> <programlisting role="php"> -<?php if ($a == 5): ?> - A vaut 5 -<?php endif; ?> + <![CDATA[ +<?php + if ($a == 5): +?> + A vaut 5 +<?php + endif; + ?> +]]> </programlisting> </informalexample> </para> @@ -189,7 +205,8 @@ <literal>else</literal> utilisant cette autre syntaxe: <informalexample> <programlisting role="php"> -<?php + <![CDATA[ +<?php if ($a == 5): print "a égale 5"; print "..."; @@ -199,7 +216,8 @@ else: print "a ne vaut ni 5 ni 6"; endif; -?> +?> +]]> </programlisting> </informalexample> </para> @@ -219,9 +237,11 @@ est le suivant : <informalexample> <programlisting role="php"> -<?php -while (expression) commandes -?> + <![CDATA[ +<?php + while (expression) commandes +?> +]]> </programlisting> </informalexample> </para> @@ -247,9 +267,11 @@ de parenthèses ou en utilisant la syntaxe suivante: <informalexample> <programlisting role="php"> -<?php + <![CDATA[ +<?php while (expression): commandes ... endwhile; -?> +?> +]]> </programlisting> </informalexample> </para> @@ -258,20 +280,22 @@ de 1 à 10: <informalexample> <programlisting role="php"> -<?php + <![CDATA[ +<?php /* exemple 1 */ $i = 1; -while ($i <= 10) { +while ($i <= 10) { print $i++; /* La valeur affiche est $i avant l'incrémentation (post-incrémentation) */ } /* exemple 2 */ $i = 1; -while ($i <= 10): +while ($i <= 10): print $i; $i++; endwhile; -?> +?> +]]> </programlisting> </informalexample> </para> @@ -295,12 +319,14 @@ Il n'y a qu'une syntaxe possible pour les boucles <literal>do..while</literal>: <informalexample> <programlisting role="php"> -<?php + <![CDATA[ +<?php $i = 0; do { print $i; } while ($i>0); -?> +?> +]]> </programlisting> </informalexample> </para> @@ -321,20 +347,22 @@ Le code suivant montre une utilisation possible: <informalexample> <programlisting role="php"> -<?php + <![CDATA[ +<?php do { - if ($i < 5) { + if ($i < 5) { print "i n'est pas suffisamment grand"; break; } $i *= $factor; - if ($i < $minimum_limit) { + if ($i < $minimum_limit) { break; } print "i est bon"; ...process i... } while(0); -?> +?> +]]> </programlisting> </informalexample> </para> @@ -353,9 +381,11 @@ La syntaxe des boucles for est la suivante: <informalexample> <programlisting role="php"> -<?php -for (expr1; expr2; expr3) statement -?> + <![CDATA[ +<?php + for (expr1; expr2; expr3) statement +?> +]]> </programlisting> </informalexample> </para> @@ -391,14 +421,15 @@ 1 à 10: <informalexample> <programlisting role="php"> -<?php + <![CDATA[ +<?php /* exemple 1 */ -for ($i = 1; $i <= 10; $i++) { +for ($i = 1; $i <= 10; $i++) { print $i; } /* exemple 2 */ for ($i = 1;;$i++) { - if ($i > 10) { + if ($i > 10) { break; } print $i; @@ -406,15 +437,16 @@ /* exemple 3 */ $i = 1; for (;;) { - if ($i > 10) { + if ($i > 10) { break; } print $i; $i++; } /* exemple 4 */ -for ($i = 1; $i <= 10; print $i, $i++) ; -?> +for ($i = 1; $i <= 10; print $i, $i++) ; +?> +]]> </programlisting> </informalexample> </para> @@ -429,9 +461,11 @@ <literal>for</literal> : <informalexample> <programlisting role="php"> -<?php -for (expr1; expr2; expr3): statement; ...; endfor; -?> + <![CDATA[ +<?php + for (expr1; expr2; expr3): statement; ...; endfor; +?> +]]> </programlisting> </informalexample> </para> @@ -455,10 +489,12 @@ une extension mineure mais pratique de la première: <informalexample> <programlisting role="php"> -<?php + <![CDATA[ +<?php foreach(array_expression as $value) commandes - foreach(array_expression as $key => $value) commandes -?> + foreach(array_expression as $key => $value) commandes +?> +]]> </programlisting> </informalexample> </para> @@ -505,29 +541,33 @@ manière identique : <informalexample> <programlisting role="php"> -<?php + <![CDATA[ +<?php reset($arr); while (list(, $value) = each ($arr)) { - echo "Valeur: $value<br>\n"; + echo "Valeur: $value<br>\n"; } foreach ($arr as $value) { - echo "Valeur: $value<br>\n"; + echo "Valeur: $value<br>\n"; } -?> +?> +]]> </programlisting> </informalexample> Les exemples suivants sont aussi fonctionnellement identiques : <informalexample> <programlisting role="php"> -<?php + <![CDATA[ +<?php reset($arr); while (list($key, $value) = each ($arr)) { - echo "Clé: $key; Valeur: $value<br>\n"; + echo "Clé: $key; Valeur: $value<br>\n"; } - foreach ($arr as $key => $value) { - echo "Clé: $key; Valeur: $value<br>\n"; + foreach ($arr as $key => $value) { + echo "Clé: $key; Valeur: $value<br>\n"; } -?> +?> +]]> </programlisting> </informalexample> </para> @@ -535,7 +575,8 @@ Voici quelques exemples de plus : <informalexample> <programlisting role="php"> -<?php + <![CDATA[ +<?php /* exemple 1: valeurs seules */ $a = array (1, 2, 3, 17); foreach ($a as $v) { @@ -545,17 +586,17 @@ $a = array (1, 2, 3, 17); $i = 0; /* pour l'illustration uniquement */ foreach($a as $v) { - print "\$a[$i] => $v.\n"; + print "\$a[$i] => $v.\n"; } /* exemple 3: clé et valeur */ $a = array ( - "un" => 1, - "deux" => 2, - "trois" => 3, - "dix-sept" => 17 + "un" => 1, + "deux" => 2, + "trois" => 3, + "dix-sept" => 17 ); -foreach($a as $k => $v) { - print "\$a[$k] => $v.\n"; +foreach($a as $k => $v) { + print "\$a[$k] => $v.\n"; } /* exemple 4: tableaux multi-dimensionnels */ $a[0][0] = "a"; @@ -571,7 +612,8 @@ foreach(array(1, 2, 3, 4, 5) as $v) { print "$v\n"; } -?> +?> +]]> </programlisting> </informalexample> </para> @@ -591,9 +633,10 @@ <para> <informalexample> <programlisting role="php"> -<?php + <![CDATA[ +<?php $i = 0; -while ($i < 10) { +while ($i < 10) { if ($arr[$i] == "stop") { break; /* Vous pouvez aussi écrire 'break 1;' ici. */ } @@ -604,16 +647,17 @@ while ( ++$i ) { switch ( $i ) { case 5: - echo "à 5<br>\n"; + echo "à 5<br>\n"; break 1; /* Ne sort que du switch. */ case 10: - echo "à 10; quitting<br>\n"; + echo "à 10; quitting<br>\n"; break 2; /* Sort du switch et du while. */ default: break; } } -?> +?> +]]> </programlisting> </informalexample> </para> @@ -634,7 +678,8 @@ <para> <informalexample> <programlisting role="php"> -<?php + <![CDATA[ +<?php while (list ($cle, $valeur) = each ($arr)) { if (!($cle % 2)) { // évite les membres impairs continue; @@ -642,19 +687,20 @@ fonction_quelconque($valeur); } $i = 0; -while ($i++ < 5) { - echo "Dehors<br>\n"; +while ($i++ < 5) { + echo "Dehors<br>\n"; while (1) { - echo " Milieu<br>\n"; + echo " Milieu<br>\n"; while (1) { - echo " Intérieur<br>\n"; + echo " Intérieur<br>\n"; continue 3; } - echo "Ceci n'est jamais atteint.<br>\n"; + echo "Ceci n'est jamais atteint.<br>\n"; } - echo "Ceci non plus.<br>\n"; + echo "Ceci non plus.<br>\n"; } -?> +?> +]]> </programlisting> </informalexample> </para> @@ -678,7 +724,8 @@ et l'autre en utilisant l'instruction <literal>switch</literal>: <informalexample> <programlisting role="php"> -<?php + <![CDATA[ +<?php if ($i == 0) { print "i égale 0"; } @@ -699,7 +746,8 @@ print "i égale 2"; break; } -?> +?> +]]> </programlisting> </informalexample> </para> @@ -720,7 +768,8 @@ toutes les instructions qui suivent. Par exemple : <informalexample> <programlisting role="php"> -<?php + <![CDATA[ +<?php switch ($i) { case 0: print "i égale 0"; @@ -729,7 +778,8 @@ case 2: print "i égale 2"; } -?> +?> +]]> </programlisting> </informalexample> </para> @@ -760,7 +810,8 @@ commandes du cas suivant. <informalexample> <programlisting role="php"> -<?php + <![CDATA[ +<?php switch ($i) { case 0: case 1: @@ -770,7 +821,8 @@ case 3: print "i égale 3"; } -?> +?> +]]> </programlisting> </informalexample> </para> @@ -781,7 +833,8 @@ être le dernier cas listé. Par exemple : <informalexample> <programlisting role="php"> -<?php + <![CDATA[ +<?php switch ($i) { case 0: print "i égale 0"; @@ -795,7 +848,8 @@ default: print "i n'est ni égal à 2, ni à 1, ni à 0."; } -?> +?> +]]> </programlisting> </informalexample> </para> @@ -812,7 +866,8 @@ <link linkend="control-structures.alternative-syntax">syntaxes alternatives</link>). <informalexample> <programlisting role="php"> -<?php + <![CDATA[ +<?php switch ($i): case 0: print "i égale 0"; @@ -826,7 +881,8 @@ default: print "i n'est ni égal à 2, ni à 1, ni à 0"; endswitch; -?> +?> +]]> </programlisting> </informalexample> </para> @@ -840,9 +896,11 @@ fonctions de contrôle : <informalexample> <programlisting> -<?php + <![CDATA[ +<?php declare (directive) statement -?> +?> +]]> </programlisting> </informalexample> </para> @@ -877,8 +935,9 @@ <para> <informalexample> <programlisting role="php"> -<PRE> -<?php +<![CDATA[ +<PRE> +<?php // Un fonction qui enregistre l'heure à laquelle elle est appelée function profile($dump = FALSE){ static $profile; @@ -896,21 +955,24 @@ profile(); // Exécute un bloc de code, et appele un tick toutes les deux secondes declare (ticks=2) { - for ($x = 1; $x < 50; ++$x) { - echo similar_text(md5($x), md5($x*$x)), "<br>"; + for ($x = 1; $x < 50; ++$x) { + echo similar_text(md5($x), md5($x*$x)), "<br>"; } } -?> +?> +]]> </programlisting> </informalexample> Pour voir le résultat : <informalexample> <programlisting role="php"> -<?php +<![CDATA[ +<?php // Affiche les données de la variable $profile print_r(profile(TRUE)); -?> -</pre> +?> +</pre> +]]> </programlisting> </informalexample> Cet exemple profile le code PHP dans le bloc de déclaration, @@ -981,9 +1043,11 @@ Pour cela, il faut utiliser <function>include</function>. <informalexample> <programlisting role="php"> -<?php +<![CDATA[ +<?php require('header.inc'); -?> +?> +]]> </programlisting> </informalexample> </para> @@ -996,14 +1060,16 @@ et vice-versa. <informalexample> <programlisting role="php"> -<?php +<![CDATA[ +<?php require ("file.inc?varone=1&vartwo=2"); /* Ne fonctionne pas. */ $varone = 1; $vartwo = 2; require ("file.inc"); /* $varone et $vartwo seront accessibles à file.inc */ -?> +?> +]]> </programlisting> </informalexample> </para> @@ -1048,12 +1114,14 @@ fois un fichier, ou même des fichiers différents. <informalexample> <programlisting role="php"> -<?php +<![CDATA[ +<?php $files = array ('premier.inc', 'second.inc', 'troisieme.inc'); -for ($i = 0; $i < count($files); $i++) { +for ($i = 0; $i < count($files); $i++) { include $files[$i]; } -?> +?> +]]> </programlisting> </informalexample> </para> @@ -1075,7 +1143,9 @@ bloc si elle est incluse dans une structure conditionnelle. <informalexample> <programlisting role="php"> -<?php +<![CDATA[ +<?php + /* Ceci est faux, et ne fonctionnera pas comme on l'attend. */ if ($condition) include($file); @@ -1087,7 +1157,8 @@ } else { include($other); } -?> +?> +]]> </programlisting> </informalexample> </para> @@ -1110,22 +1181,26 @@ et est placé dans le même dossier que le fichier principal : <programlisting role="php"> -<?php -echo "Avant le retour<br>\n"; +<![CDATA[ +<?php +echo "Avant le retour<br>\n"; if (1) { return 27; } -echo "Après le retour <br>\n"; -?> +echo "Après le retour <br>\n"; +?> +]]> </programlisting> </para> <para> On suppose que le fichier <filename>main.html</filename> contient ceci : <programlisting role="php"> -<?php +<![CDATA[ +<?php $retval = include ('test.inc'); -echo "Fichier inclus: '$retval'<br>\n"; -?> +echo "Fichier inclus: '$retval'<br>\n"; +?> +]]> </programlisting> </para> <para> @@ -1144,10 +1219,12 @@ été modifié et contient maintenant le code suivant : <programlisting role="php"> -<?php -include ('test.inc'); -echo "Retour dans le main.html<br>\n"; -?> +<![CDATA[ +<?php + include ('test.inc'); + echo "Retour dans le main.html<br>\n"; +?> +]]> </programlisting> </para> <para> @@ -1187,12 +1264,15 @@ <link linkend="language.basic-syntax.phpmode">les balises habituelles de PHP</link>. <informalexample> <programlisting role="php"> -<?php -include ("file.inc?varone=1&vartwo=2"); /* ne fonctionne pas. */ -$varone = 1; -$vartwo = 2; -include ("file.inc"); /* $varone et $vartwo sont accessibles dans file.inc */ -?> +<![CDATA[ +<?php + include ("file.inc?varone=1&vartwo=2"); /* ne fonctionne pas. */ + $varone = 1; + $vartwo = 2; + include ("file.inc"); + /* $varone et $vartwo sont accessibles dans file.inc */ +?> +]]> </programlisting> </informalexample> </para> @@ -1227,19 +1307,22 @@ <example> <title>utils.inc</title> <programlisting role="php"> -<?php +<![CDATA[ +<?php define(PHPVERSION, floor(phpversion())); echo "LES GLOBALES SONT SYMPAS\n"; function goodTea() { return "Le Earl Grey est délicieux!"; } -?> +?> +]]> </programlisting> </example> <example> <title>foolib.inc</title> <programlisting role="php"> -<?php +<![CDATA[ +<?php require ("utils.inc"); function showVar($var) { if (PHPVERSION == 4) { @@ -1249,24 +1332,27 @@ } } // Une série de fonctions -?> +?> +]]> </programlisting> </example> Puis, vous écrivez un script <literal>cause_error_require.php</literal> <example> <title>cause_error_require.php</title> <programlisting role="php"> -<?php -require("foolib.inc"); +<![CDATA[ +<?php + require("foolib.inc"); /* Ceci génère une erreur*/ -require("utils.inc"); -$foo = array("1",array("complex","quaternion")); -echo "Ce code requiert utils.inc une deuxième fois, car il est requis \n"; -echo "dans foolib.inc\n"; -echo "Utilisation de GoodTea: ".goodTea()."\n"; -echo "Affichage de foo: \n"; -showVar($foo); -?> + require("utils.inc"); + $foo = array("1",array("complex","quaternion")); + echo "Ce code requiert utils.inc une deuxième fois, car il est requis \n"; + echo "dans foolib.inc\n"; + echo "Utilisation de GoodTea: ".goodTea()."\n"; + echo "Affichage de foo: \n"; + showvar($foo); +?> +]]> </programlisting> </example> Lorsque vous exécutez le script ci-dessus, le @@ -1286,20 +1372,24 @@ <example> <title>foolib.inc (corrigé)</title> <programlisting role="php"> -<?php -require_once("utils.inc"); -function showVar($var) { -?> +<![CDATA[ +<?php + require_once("utils.inc"); + function showVar($var) { +?> +]]> </programlisting> </example> <example> <title>avoid_error_require_once.php</title> <programlisting role="php"> -<?php -require_once("foolib.inc"); -require_once("utils.inc"); -$foo = array("1",array("complexe","quaternion")); -?> +<![CDATA[ +<?php + require_once("foolib.inc"); + require_once("utils.inc"); + $foo = array("1",array("complexe","quaternion")); +?> +]]> </programlisting> </example> L'exécution de ce script, sous PHP 4.0.1pl2, donne : @@ -1312,11 +1402,11 @@ Affichage de foo: Array ( - [0] => 1 - [1] => Array + [0] => 1 + [1] => Array ( - [0] => complexe - [1] => quaternion + [0] => complexe + [1] => quaternion ) ) </programlisting>