cvs: phpdoc-fr /language control-structures.xml

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