[DOC-CVS] [doc-en] master: Enable runnable (WASM) examples for language.control-structures section (#5462)
[email protected] (AllenJB via GitHub) Mon, 20 Jul 2026 10:50:03 +0000
| Newsgroups | php.doc.cvs |
|---|---|
| Message-ID | <[email protected]> |
Author: AllenJB (AllenJB) Committer: GitHub (web-flow) Pusher: jordikroon Date: 2026-07-20T12:50:00+02:00 Commit: https://github.com/php/doc-en/commit/b8147b7f03506e41f01631c32a907b955593525a Raw diff: https://github.com/php/doc-en/commit/b8147b7f03506e41f01631c32a907b955593525a.diff Enable runnable (WASM) examples for language.control-structures section (#5462) * Enable runnable (WASM) examples for language.control-structures section Co-authored-by: Louis-Arnaud <[email protected]> Changed paths: M language/control-structures.xml M language/control-structures/alternative-syntax.xml M language/control-structures/break.xml M language/control-structures/continue.xml M language/control-structures/declare.xml M language/control-structures/do-while.xml M language/control-structures/else.xml M language/control-structures/elseif.xml M language/control-structures/for.xml M language/control-structures/foreach.xml M language/control-structures/goto.xml M language/control-structures/if.xml M language/control-structures/include.xml M language/control-structures/match.xml M language/control-structures/switch.xml M language/control-structures/while.xml Diff: diff --git a/language/control-structures.xml b/language/control-structures.xml index 8adff8a75691..b9a6b334e9b5 100644 --- a/language/control-structures.xml +++ b/language/control-structures.xml @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="utf-8"?> <!-- $Revision$ --> -<chapter xml:id="language.control-structures" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink"> +<chapter xml:id="language.control-structures" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" annotations="interactive"> <title>Control Structures</title> <sect1 xml:id="control-structures.intro"> diff --git a/language/control-structures/alternative-syntax.xml b/language/control-structures/alternative-syntax.xml index 185d0a2e04b2..0ab3e5d9fddd 100644 --- a/language/control-structures/alternative-syntax.xml +++ b/language/control-structures/alternative-syntax.xml @@ -17,11 +17,19 @@ <informalexample> <programlisting role="php"> <![CDATA[ -<?php if ($a == 5): ?> +<?php +$a = 5; +if ($a == 5): ?> A is equal to 5 <?php endif; ?> ]]> </programlisting> + &example.outputs; + <screen> +<![CDATA[ +A is equal to 5 +]]> + </screen> </informalexample> </para> <simpara> @@ -38,6 +46,7 @@ A is equal to 5 <programlisting role="php"> <![CDATA[ <?php +$a = 5; if ($a == 5): echo "a equals 5"; echo "..."; @@ -47,9 +56,14 @@ elseif ($a == 6): else: echo "a is neither 5 nor 6"; endif; -?> ]]> </programlisting> + &example.outputs; + <screen> +<![CDATA[ +a equals 5... +]]> + </screen> </informalexample> </para> <note> @@ -66,12 +80,20 @@ endif; <informalexample> <programlisting role="php"> <![CDATA[ -<?php switch ($foo): ?> +<?php +$foo = 1; +switch ($foo): ?> <?php case 1: ?> - // ... + Foo is 1 <?php endswitch; ?> ]]> </programlisting> + &example.outputs; + <screen> +<![CDATA[ +Parse error: syntax error, unexpected T_INLINE_HTML " ", expecting "endswitch" or "case" or "default" in script on line 4 +]]> + </screen> </informalexample> <para> Whereas this is valid, as the trailing newline after the @@ -82,12 +104,20 @@ endif; <informalexample> <programlisting role="php"> <![CDATA[ -<?php switch ($foo): ?> +<?php +$foo = 1; +switch ($foo): ?> <?php case 1: ?> - ... + Foo is 1 <?php endswitch; ?> ]]> </programlisting> + &example.outputs; + <screen> +<![CDATA[ + Foo is 1 +]]> + </screen> </informalexample> </warning> <para> diff --git a/language/control-structures/break.xml b/language/control-structures/break.xml index 4e1b7f2fb23c..7b49cb79da6f 100644 --- a/language/control-structures/break.xml +++ b/language/control-structures/break.xml @@ -26,27 +26,61 @@ foreach ($arr as $val) { if ($val == 'stop') { break; /* You could also write 'break 1;' here. */ } - echo "$val<br />\n"; + echo "$val\n"; } - -/* Using the optional argument. */ - +]]> + </programlisting> + &example.outputs; + <screen> +<![CDATA[ +one +two +three +four +]]> + </screen> + </informalexample> + </para> + <simpara> + Using the optional argument: + </simpara> + <para> + <informalexample> + <programlisting role="php"> +<![CDATA[ +<?php $i = 0; while (++$i) { switch ($i) { case 5: - echo "At 5<br />\n"; + echo "At 5\n"; break 1; /* Exit only the switch. */ case 10: - echo "At 10; quitting<br />\n"; + echo "At 10; quitting\n"; break 2; /* Exit the switch and the while. */ default: break; } + echo "$i\n"; } -?> ]]> </programlisting> + &example.outputs; + <screen> +<![CDATA[ +1 +2 +3 +4 +At 5 +5 +6 +7 +8 +9 +At 10; quitting +]]> + </screen> </informalexample> </para> diff --git a/language/control-structures/continue.xml b/language/control-structures/continue.xml index 0635a484b60c..14052a1b4f23 100644 --- a/language/control-structures/continue.xml +++ b/language/control-structures/continue.xml @@ -16,7 +16,7 @@ considered a looping structure for the purposes of <literal>continue</literal>. <literal>continue</literal> behaves like <literal>break</literal> (when no arguments are passed) but will - raise a warning as this is likely to be a mistake. If a + raise a warning as this is likely to be a mistake. If a <literal>switch</literal> is inside a loop, <literal>continue 2</literal> will continue with the next iteration of the outer loop. @@ -40,7 +40,6 @@ foreach ($arr as $key => $value) { } echo $value . "\n"; } -?> ]]> </programlisting> &examples.outputs; @@ -67,7 +66,6 @@ while ($i++ < 5) { } echo "Neither does this.\n"; } -?> ]]> </programlisting> &examples.outputs; @@ -88,36 +86,6 @@ Inner Outer Middle Inner -]]> - </screen> - </informalexample> - </para> - <para> - Omitting the semicolon after <literal>continue</literal> can lead to - confusion. Here's an example of what you shouldn't do. - </para> - <para> - <informalexample> - <programlisting role="php"> -<![CDATA[ -<?php -for ($i = 0; $i < 5; ++$i) { - if ($i == 2) - continue - print "$i\n"; -} -?> -]]> - </programlisting> - <para> - One can expect the result to be: - </para> - <screen> -<![CDATA[ -0 -1 -3 -4 ]]> </screen> </informalexample> @@ -136,7 +104,7 @@ for ($i = 0; $i < 5; ++$i) { <row> <entry>7.3.0</entry> <entry> - <literal>continue</literal> within a <literal>switch</literal> that is attempting to act like a <literal>break</literal> statement for the + <literal>continue</literal> within a <literal>switch</literal> that is attempting to act like a <literal>break</literal> statement for the <literal>switch</literal> will trigger an <constant>E_WARNING</constant>. </entry> </row> diff --git a/language/control-structures/declare.xml b/language/control-structures/declare.xml index 0b4f992a5f1a..8924ea0bc8a3 100644 --- a/language/control-structures/declare.xml +++ b/language/control-structures/declare.xml @@ -36,7 +36,7 @@ declare (directive) be given as directive values. Variables and constants cannot be used. To illustrate: <informalexample> - <programlisting role="php"> + <programlisting role="php" annotations="non-interactive"> <![CDATA[ <?php // This is valid: @@ -45,7 +45,6 @@ declare(ticks=1); // This is invalid: const TICK_VALUE = 1; declare(ticks=TICK_VALUE); -?> ]]> </programlisting> </informalexample> @@ -63,7 +62,7 @@ declare(ticks=TICK_VALUE); <literal>declare</literal> was included then it does not affect the parent file). <informalexample> - <programlisting role="php"> + <programlisting role="php" annotations="non-interactive"> <![CDATA[ <?php // these are the same: @@ -76,7 +75,6 @@ declare(ticks=1) { // or you can use this: declare(ticks=1); // entire script here -?> ]]> </programlisting> </informalexample> @@ -123,12 +121,20 @@ $a = 1; // causes a tick event if ($a > 0) { $a += 2; // causes a tick event - print $a; // causes a tick event + print $a . "\n"; // causes a tick event } - -?> ]]> </programlisting> + &example.outputs; + <screen> +<![CDATA[ +tick_handler() called +tick_handler() called +tick_handler() called +3 +tick_handler() called +]]> + </screen> </example> </para> <simpara> @@ -142,12 +148,11 @@ if ($a > 0) { A script's encoding can be specified per-script using the <literal>encoding</literal> directive. <example> <title>Declaring an encoding for the script</title> - <programlisting role="php"> + <programlisting role="php" annotations="non-interactive"> <![CDATA[ <?php declare(encoding='ISO-8859-1'); // code here -?> ]]> </programlisting> </example> diff --git a/language/control-structures/do-while.xml b/language/control-structures/do-while.xml index 57ba3892a54f..0bc2d6447d1c 100644 --- a/language/control-structures/do-while.xml +++ b/language/control-structures/do-while.xml @@ -28,9 +28,14 @@ $i = 0; do { echo $i; } while ($i > 0); -?> ]]> </programlisting> + &example.outputs; + <screen> +<![CDATA[ +0 +]]> + </screen> </informalexample> </para> <simpara> @@ -50,23 +55,32 @@ do { <programlisting role="php"> <![CDATA[ <?php +$i = 50; +$factor = 0.5; +$minimum_limit = 20; do { if ($i < 5) { - echo "i is not big enough"; + echo "i is not big enough\n"; break; } $i *= $factor; if ($i < $minimum_limit) { + echo "i is less than minimum limit after factor\n"; break; } - echo "i is ok"; + echo $i ." is ok\n"; /* process i */ } while (0); -?> ]]> </programlisting> + &example.outputs; + <screen> +<![CDATA[ +25 is ok +]]> + </screen> </informalexample> </para> <simpara> diff --git a/language/control-structures/else.xml b/language/control-structures/else.xml index dfe7db2b4f41..48be04238e81 100644 --- a/language/control-structures/else.xml +++ b/language/control-structures/else.xml @@ -19,14 +19,21 @@ <programlisting role="php"> <![CDATA[ <?php +$a = 3; +$b = 5; if ($a > $b) { - echo "a is greater than b"; + echo "a is greater than b\n"; } else { - echo "a is NOT greater than b"; + echo "a is NOT greater than b\n"; } -?> ]]> </programlisting> + &example.outputs; + <screen> +<![CDATA[ +a is NOT greater than b +]]> + </screen> </informalexample> The <literal>else</literal> statement is only executed if the @@ -53,9 +60,14 @@ if ($a) echo "b"; else echo "c"; -?> ]]> </programlisting> + &example.outputs; + <screen> +<![CDATA[ + +]]> + </screen> </informalexample> Despite the indentation (which does not matter for PHP), the <literal>else</literal> is associated with the <literal>if ($b)</literal>, so the example does not produce diff --git a/language/control-structures/elseif.xml b/language/control-structures/elseif.xml index 5685c704d3de..dcfd0cecf04f 100644 --- a/language/control-structures/elseif.xml +++ b/language/control-structures/elseif.xml @@ -21,6 +21,8 @@ <programlisting role="php"> <![CDATA[ <?php +$a = 1; +$b = 1; if ($a > $b) { echo "a is bigger than b"; } elseif ($a == $b) { @@ -28,9 +30,14 @@ if ($a > $b) { } else { echo "a is smaller than b"; } -?> ]]> </programlisting> + &example.outputs; + <screen> +<![CDATA[ +a is equal to b +]]> + </screen> </informalexample> </para> <simpara> @@ -67,6 +74,8 @@ if ($a > $b) { <programlisting role="php"> <![CDATA[ <?php +$a = 3; +$b = 1; /* Incorrect Method: */ if ($a > $b): @@ -76,12 +85,21 @@ else if ($a == $b): // Will not compile. endif; ]]> </programlisting> + &example.outputs; + <screen> +<![CDATA[ +Parse error: syntax error, unexpected token "if", expecting ":" in script on line 8 +]]> + </screen> </informalexample> <informalexample> <programlisting role="php"> <![CDATA[ <?php +$a = 3; +$b = 1; + /* Correct Method: */ if ($a > $b): echo $a." is greater than ".$b; @@ -90,10 +108,14 @@ elseif ($a == $b): // Note the combination of the words. else: echo $a." is neither greater than or equal to ".$b; endif; - -?> ]]> </programlisting> + &example.outputs; + <screen> +<![CDATA[ +3 is greater than 1 +]]> + </screen> </informalexample> </para> </sect1> diff --git a/language/control-structures/for.xml b/language/control-structures/for.xml index 14c11af74763..0abadadd426a 100644 --- a/language/control-structures/for.xml +++ b/language/control-structures/for.xml @@ -54,38 +54,71 @@ for (expr1; expr2; expr3) <programlisting role="php"> <![CDATA[ <?php -/* example 1 */ - for ($i = 1; $i <= 10; $i++) { - echo $i; + echo $i . " "; } - -/* example 2 */ - +]]> + </programlisting> + &example.outputs; + <screen> +<![CDATA[ +1 2 3 4 5 6 7 8 9 10 +]]> + </screen> + </informalexample> + <informalexample> + <programlisting role="php"> +<![CDATA[ +<?php for ($i = 1; ; $i++) { if ($i > 10) { break; } - echo $i; + echo $i . " "; } - -/* example 3 */ - +]]> + </programlisting> + &example.outputs; + <screen> +<![CDATA[ +1 2 3 4 5 6 7 8 9 10 +]]> + </screen> + </informalexample> + <informalexample> + <programlisting role="php"> +<![CDATA[ +<?php $i = 1; for (; ; ) { if ($i > 10) { break; } - echo $i; + echo $i . " "; $i++; } - -/* example 4 */ - -for ($i = 1, $j = 0; $i <= 10; $j += $i, print $i, $i++); -?> ]]> </programlisting> + &example.outputs; + <screen> +<![CDATA[ +1 2 3 4 5 6 7 8 9 10 +]]> + </screen> + </informalexample> + <informalexample> + <programlisting role="php"> +<![CDATA[ +<?php +for ($i = 1, $j = 0; $i <= 10; $j += $i, print $i . " ", $i++); +]]> + </programlisting> + &example.outputs; + <screen> +<![CDATA[ +1 2 3 4 5 6 7 8 9 10 +]]> + </screen> </informalexample> </para> <simpara> @@ -127,11 +160,32 @@ $people = array( ); for($i = 0; $i < count($people); ++$i) { - $people[$i]['salt'] = mt_rand(000000, 999999); + $people[$i]['salt'] = random_int(100000, 999999); } -?> +var_dump($people); ]]> </programlisting> + &example.outputs.similar; + <screen> +<![CDATA[ +array(2) { + [0]=> + array(2) { + ["name"]=> + string(5) "Kalle" + ["salt"]=> + int(454478) + } + [1]=> + array(2) { + ["name"]=> + string(6) "Pierre" + ["salt"]=> + int(776978) + } +} +]]> + </screen> </informalexample> </para> <simpara> @@ -151,11 +205,32 @@ $people = array( ); for($i = 0, $size = count($people); $i < $size; ++$i) { - $people[$i]['salt'] = mt_rand(000000, 999999); + $people[$i]['salt'] = random_int(100000, 999999); } -?> +var_dump($people); ]]> </programlisting> + &example.outputs.similar; + <screen> +<![CDATA[ +array(2) { + [0]=> + array(2) { + ["name"]=> + string(5) "Kalle" + ["salt"]=> + int(454478) + } + [1]=> + array(2) { + ["name"]=> + string(6) "Pierre" + ["salt"]=> + int(776978) + } +} +]]> + </screen> </informalexample> </para> </sect1> diff --git a/language/control-structures/foreach.xml b/language/control-structures/foreach.xml index 8cffaca5fa24..ded0de4dee78 100644 --- a/language/control-structures/foreach.xml +++ b/language/control-structures/foreach.xml @@ -50,14 +50,14 @@ foreach (iterable_expression as $key => $value) { <![CDATA[ <?php -/* Example: value only */ +echo "Value only\n"; $array = [1, 2, 3, 17]; foreach ($array as $value) { echo "Current element of \$array: $value.\n"; } -/* Example: key and value */ +echo "\n\nKey and value:\n"; $array = [ "one" => 1, "two" => 2, @@ -69,7 +69,7 @@ foreach ($array as $key => $value) { echo "Key: $key => Value: $value\n"; } -/* Example: multi-dimensional key-value arrays */ +echo "\n\nMulti-dimensional key-value arrays:\n"; $grid = []; $grid[0][0] = "a"; $grid[0][1] = "b"; @@ -82,13 +82,44 @@ foreach ($grid as $y => $row) { } } -/* Example: dynamic arrays */ +echo "\n\nDynamic arrays:\n"; foreach (range(1, 5) as $value) { echo "$value\n"; } -?> ]]> </programlisting> + &example.outputs; + <screen> +<![CDATA[ +Value only +Current element of $array: 1. +Current element of $array: 2. +Current element of $array: 3. +Current element of $array: 17. + + +Key and value: +Key: one => Value: 1 +Key: two => Value: 2 +Key: three => Value: 3 +Key: seventeen => Value: 17 + + +Multi-dimensional key-value arrays: +Value at position x=0 and y=0: a +Value at position x=1 and y=0: b +Value at position x=0 and y=1: y +Value at position x=1 and y=1: z + + +Dynamic arrays: +1 +2 +3 +4 +5 +]]> + </screen> </example> <note> <para> @@ -139,7 +170,6 @@ foreach ($array as [$a, $b]) { foreach ($array as list($a, $b)) { echo "A: $a; B: $b\n"; } -?> ]]> </programlisting> &example.outputs; @@ -174,7 +204,6 @@ foreach ($array as [, , $c]) { // Skipping over $a and $b echo "$c\n"; } -?> ]]> </programlisting> &example.outputs; @@ -205,7 +234,6 @@ $array = [ foreach ($array as [$a, $b, $c]) { echo "A: $a; B: $b; C: $c\n"; } -?> ]]> </programlisting> &example.outputs; @@ -238,10 +266,25 @@ foreach ($arr as &$value) { $value = $value * 2; } // $arr is now [2, 4, 6, 8] +var_dump($arr); unset($value); // break the reference with the last element -?> ]]> </programlisting> + &example.outputs; + <screen> +<![CDATA[ +array(4) { + [0]=> + int(2) + [1]=> + int(4) + [2]=> + int(6) + [3]=> + &int(8) +} +]]> + </screen> </informalexample> </para> <warning> @@ -260,25 +303,48 @@ foreach ($arr as &$value) { $value = $value * 2; } // $arr is now [2, 4, 6, 8] +var_dump($arr); // without an unset($value), $value is still a reference to the last item: $arr[3] +echo "\nAnother loop:\n"; foreach ($arr as $key => $value) { // $arr[3] will be updated with each value from $arr... - echo "{$key} => {$value} "; - print_r($arr); + echo "{$key} => {$value}\n"; } // ...until ultimately the second-to-last value is copied onto the last value -?> +var_dump($arr); ]]> </programlisting> &example.outputs; <screen> <![CDATA[ -0 => 2 Array ( [0] => 2, [1] => 4, [2] => 6, [3] => 2 ) -1 => 4 Array ( [0] => 2, [1] => 4, [2] => 6, [3] => 4 ) -2 => 6 Array ( [0] => 2, [1] => 4, [2] => 6, [3] => 6 ) -3 => 6 Array ( [0] => 2, [1] => 4, [2] => 6, [3] => 6 ) +array(4) { + [0]=> + int(2) + [1]=> + int(4) + [2]=> + int(6) + [3]=> + &int(8) +} + +Another loop: +0 => 2 +1 => 4 +2 => 6 +3 => 6 +array(4) { + [0]=> + int(2) + [1]=> + int(4) + [2]=> + int(6) + [3]=> + &int(6) +} ]]> </screen> </informalexample> @@ -290,10 +356,19 @@ foreach ($arr as $key => $value) { <?php foreach ([1, 2, 3, 4] as &$value) { $value = $value * 2; + echo $value . "\n"; } -?> ]]> </programlisting> + &example.outputs; + <screen> +<![CDATA[ +2 +4 +6 +8 +]]> + </screen> </example> </sect2> diff --git a/language/control-structures/goto.xml b/language/control-structures/goto.xml index b68d4ca2b6c6..9422253da580 100644 --- a/language/control-structures/goto.xml +++ b/language/control-structures/goto.xml @@ -35,14 +35,11 @@ <programlisting role="php"> <![CDATA[ <?php - goto a; echo 'Foo'; a: echo 'Bar'; - -?> ]]> </programlisting> &example.outputs; @@ -59,7 +56,6 @@ Bar <programlisting role="php"> <![CDATA[ <?php - for ($i = 0, $j = 50; $i < 100; $i++) { while ($j--) { if ($j == 17) { @@ -70,8 +66,6 @@ for ($i = 0, $j = 50; $i < 100; $i++) { echo "i = $i"; end: echo 'j hit 17'; - -?> ]]> </programlisting> &example.outputs; @@ -88,7 +82,6 @@ j hit 17 <programlisting role="php"> <![CDATA[ <?php - goto loop; for ($i = 0, $j = 50; $i < 100; $i++) { while ($j--) { @@ -96,15 +89,12 @@ for ($i = 0, $j = 50; $i < 100; $i++) { } } echo "$i = $i"; - -?> ]]> </programlisting> &example.outputs; <screen> <![CDATA[ -Fatal error: 'goto' into loop or switch statement is disallowed in -script on line 2 +Fatal error: 'goto' into loop or switch statement is disallowed in script on line 2 ]]> </screen> </example> diff --git a/language/control-structures/if.xml b/language/control-structures/if.xml index 386aafe7705e..c34687b1d67b 100644 --- a/language/control-structures/if.xml +++ b/language/control-structures/if.xml @@ -36,11 +36,18 @@ if (expr) <programlisting role="php"> <![CDATA[ <?php +$a = 3; +$b = 1; if ($a > $b) - echo "a is bigger than b"; -?> + echo "a is bigger than b\n"; ]]> </programlisting> + &example.outputs; + <screen> +<![CDATA[ +a is bigger than b +]]> + </screen> </informalexample> </para> <para> @@ -56,13 +63,22 @@ if ($a > $b) <programlisting role="php"> <![CDATA[ <?php +$a = 3; +$b = 1; if ($a > $b) { - echo "a is bigger than b"; + echo "a is bigger than b\n"; $b = $a; } -?> +var_dump($b); ]]> </programlisting> + &example.outputs; + <screen> +<![CDATA[ +a is bigger than b +int(3) +]]> + </screen> </informalexample> </para> <simpara> diff --git a/language/control-structures/include.xml b/language/control-structures/include.xml index d2de8cb4450d..10b95a0943c1 100644 --- a/language/control-structures/include.xml +++ b/language/control-structures/include.xml @@ -54,7 +54,7 @@ <para> <example> <title>Basic <literal>include</literal> example</title> - <programlisting role="php"> + <programlisting role="php" annotations="non-interactive"> <![CDATA[ vars.php <?php @@ -90,7 +90,7 @@ echo "A $color $fruit"; // A green apple <para> <example> <title>Including within functions</title> - <programlisting role="php"> + <programlisting role="php" annotations="non-interactive"> <![CDATA[ <?php @@ -110,8 +110,6 @@ function foo() foo(); // A green apple echo "A $color $fruit"; // A green - -?> ]]> </programlisting> </example> @@ -140,7 +138,7 @@ echo "A $color $fruit"; // A green <para> <example> <title><literal>include</literal> through HTTP</title> - <programlisting role="php"> + <programlisting role="php" annotations="non-interactive"> <![CDATA[ <?php @@ -157,7 +155,6 @@ include 'file.php?foo=1&bar=2'; // Works. include 'http://www.example.com/file.php?foo=1&bar=2'; -?> ]]> </programlisting> </example> @@ -200,7 +197,7 @@ include 'http://www.example.com/file.php?foo=1&bar=2'; return value. <example> <title>Comparing return value of include</title> - <programlisting role="php"> + <programlisting role="php" annotations="non-interactive"> <![CDATA[ <?php // won't work, evaluated as include(('vars.php') == TRUE), i.e. include('1') @@ -212,7 +209,6 @@ if (include('vars.php') == TRUE) { if ((include 'vars.php') == TRUE) { echo 'OK'; } -?> ]]> </programlisting> </example> @@ -220,7 +216,7 @@ if ((include 'vars.php') == TRUE) { <para> <example> <title><literal>include</literal> and the <function>return</function> statement</title> - <programlisting role="php"> + <programlisting role="php" annotations="non-interactive"> <![CDATA[ return.php <?php @@ -278,7 +274,7 @@ echo $bar; // prints 1 <para> <example> <title>Using output buffering to include a PHP file into a string</title> - <programlisting role="php"> + <programlisting role="php" annotations="non-interactive"> <![CDATA[ <?php $string = get_include_contents('somefile.php'); @@ -291,8 +287,6 @@ function get_include_contents($filename) { } return false; } - -?> ]]> </programlisting> </example> diff --git a/language/control-structures/match.xml b/language/control-structures/match.xml index cefc327413ea..9d663afe0e07 100644 --- a/language/control-structures/match.xml +++ b/language/control-structures/match.xml @@ -18,14 +18,13 @@ <example> <title>Structure of a <literal>match</literal> expression</title> - <programlisting role="php"> + <programlisting role="php" annotations="non-interactive"> <![CDATA[ <?php $return_value = match (subject_expression) { single_conditional_expression => return_expression, conditional_expression1, conditional_expression2 => return_expression, }; -?> ]]> </programlisting> @@ -43,7 +42,6 @@ $return_value = match ($food) { }; var_dump($return_value); -?> ]]> </programlisting> &example.outputs; @@ -70,7 +68,6 @@ $output = match (true) { }; var_dump($output); -?> ]]> </programlisting> &example.outputs; @@ -135,7 +132,7 @@ string(8) "Teenager" expression will be evaluated. For example: <informalexample> - <programlisting role="php"> + <programlisting role="php" annotations="non-interactive"> <![CDATA[ <?php $result = match ($x) { @@ -144,7 +141,6 @@ $result = match ($x) { $this->baz => beep(), // beep() isn't called unless $x === $this->baz // etc. }; -?> ]]> </programlisting> </informalexample> @@ -157,7 +153,7 @@ $result = match ($x) { </para> <para> <informalexample> - <programlisting role="php"> + <programlisting role="php" annotations="non-interactive"> <![CDATA[ <?php $result = match ($x) { @@ -168,7 +164,6 @@ $result = match ($x) { $b => 5, $c => 5, }; -?> ]]> </programlisting> </informalexample> @@ -178,7 +173,7 @@ $result = match ($x) { This pattern matches anything that wasn't previously matched. For example: <informalexample> - <programlisting role="php"> + <programlisting role="php" annotations="non-interactive"> <![CDATA[ <?php $expressionResult = match ($condition) { @@ -186,7 +181,6 @@ $expressionResult = match ($condition) { 3, 4 => bar(), default => baz(), }; -?> ]]> </programlisting> </informalexample> @@ -219,7 +213,6 @@ try { } catch (\UnhandledMatchError $e) { var_dump($e); } -?> ]]> </programlisting> &example.outputs; @@ -227,13 +220,13 @@ try { <![CDATA[ object(UnhandledMatchError)#1 (7) { ["message":protected]=> - string(33) "Unhandled match value of type int" + string(22) "Unhandled match case 5" ["string":"Error":private]=> string(0) "" ["code":protected]=> int(0) ["file":protected]=> - string(9) "/in/ICgGK" + string(6) "script" ["line":protected]=> int(6) ["trace":"Error":private]=> @@ -259,7 +252,6 @@ object(UnhandledMatchError)#1 (7) { <programlisting role="php"> <![CDATA[ <?php - $age = 23; $result = match (true) { @@ -270,7 +262,6 @@ $result = match (true) { }; var_dump($result); -?> ]]> </programlisting> &example.outputs; @@ -296,7 +287,6 @@ $result = match (true) { }; var_dump($result); -?> ]]> </programlisting> &example.outputs; diff --git a/language/control-structures/switch.xml b/language/control-structures/switch.xml index 2e5e06a86a84..3b4303a4a7f0 100644 --- a/language/control-structures/switch.xml +++ b/language/control-structures/switch.xml @@ -38,32 +38,39 @@ <programlisting role="php"> <![CDATA[ <?php +$i = 1; + // This switch statement: switch ($i) { case 0: - echo "i equals 0"; + echo "i equals 0\n"; break; case 1: - echo "i equals 1"; + echo "i equals 1\n"; break; case 2: - echo "i equals 2"; + echo "i equals 2\n"; break; } // Is equivalent to: - if ($i == 0) { - echo "i equals 0"; + echo "i equals 0\n"; } elseif ($i == 1) { - echo "i equals 1"; + echo "i equals 1\n"; } elseif ($i == 2) { - echo "i equals 2"; + echo "i equals 2\n"; } -?> ]]> </programlisting> + &example.outputs; + <screen> +<![CDATA[ +i equals 1 +i equals 1 +]]> + </screen> </example> </para> <para> @@ -84,17 +91,25 @@ if ($i == 0) { <programlisting role="php"> <![CDATA[ <?php +$i = 1; + switch ($i) { case 0: - echo "i equals 0"; + echo "i equals 0\n"; case 1: - echo "i equals 1"; + echo "i equals 1\n"; case 2: - echo "i equals 2"; + echo "i equals 2\n"; } -?> ]]> </programlisting> + &example.outputs; + <screen> +<![CDATA[ +i equals 1 +i equals 2 +]]> + </screen> </informalexample> </para> <simpara> @@ -121,6 +136,8 @@ switch ($i) { <programlisting role="php"> <![CDATA[ <?php +$i = 1; + switch ($i) { case 0: case 1: @@ -130,9 +147,14 @@ switch ($i) { case 3: echo "i is 3"; } -?> ]]> </programlisting> + &example.outputs; + <screen> +<![CDATA[ +i is less than 3 but not negative +]]> + </screen> </informalexample> </para> <para> @@ -142,6 +164,8 @@ switch ($i) { <programlisting role="php"> <![CDATA[ <?php +$i = 4; + switch ($i) { case 0: echo "i equals 0"; @@ -155,9 +179,14 @@ switch ($i) { default: echo "i is not equal to 0, 1 or 2"; } -?> ]]> </programlisting> + &example.outputs; + <screen> +<![CDATA[ +i is not equal to 0, 1 or 2 +]]> + </screen> </informalexample> <note> <simpara> @@ -203,11 +232,14 @@ switch ($target) { print "D"; break; } - -// Prints "B" -?> ]]> </programlisting> + &example.outputs; + <screen> +<![CDATA[ +B +]]> + </screen> </informalexample> </para> <para> @@ -215,7 +247,7 @@ switch ($target) { Or, alternatively, <literal>if</literal>-<literal>else</literal> blocks instead of <literal>switch</literal>. <informalexample> <programlisting role="php"> - <![CDATA[ +<![CDATA[ <?php $offset = 1; $start = 3; @@ -234,11 +266,14 @@ switch (true) { print "D"; break; } - -// Prints "B" -?> ]]> </programlisting> + &example.outputs; + <screen> +<![CDATA[ +B +]]> + </screen> </informalexample> </para> <para> @@ -250,6 +285,8 @@ switch (true) { <programlisting role="php"> <![CDATA[ <?php +$i = 1; + switch ($i): case 0: echo "i equals 0"; @@ -263,17 +300,25 @@ switch ($i): default: echo "i is not equal to 0, 1 or 2"; endswitch; -?> ]]> </programlisting> + &example.outputs; + <screen> +<![CDATA[ +i equals 1 +]]> + </screen> </informalexample> </para> <para> - It's possible to use a semicolon instead of a colon after a case like: + It's possible to use a semicolon instead of a colon after a case. + As of PHP 8.5.0, this syntax is deprecated. <informalexample> <programlisting role="php"> <![CDATA[ <?php +$beer = 'cola'; + switch($beer) { case 'tuborg'; @@ -286,9 +331,22 @@ switch($beer) echo 'Please make a new selection...'; break; } -?> ]]> </programlisting> + &example.outputs; + <screen> +<![CDATA[ + +Deprecated: Case statements followed by a semicolon (;) are deprecated, use a colon (:) instead in script on line 6 + +Deprecated: Case statements followed by a semicolon (;) are deprecated, use a colon (:) instead in script on line 7 + +Deprecated: Case statements followed by a semicolon (;) are deprecated, use a colon (:) instead in script on line 8 + +Deprecated: Case statements followed by a semicolon (;) are deprecated, use a colon (:) instead in script on line 11 +Please make a new selection... +]]> + </screen> </informalexample> &warn.deprecated.feature-8-5-0; </para> diff --git a/language/control-structures/while.xml b/language/control-structures/while.xml index 7551356875b9..e0916a8978d4 100644 --- a/language/control-structures/while.xml +++ b/language/control-structures/while.xml @@ -53,25 +53,56 @@ endwhile; <programlisting role="php"> <![CDATA[ <?php -/* example 1 */ - $i = 1; while ($i <= 10) { - echo $i++; /* the printed value would be - $i before the increment - (post-increment) */ + echo $i++ . "\n"; /* the printed value would be + $i before the increment + (post-increment) */ } - -/* example 2 */ - +]]> + </programlisting> + &example.outputs; + <screen> +<![CDATA[ +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 +]]> + </screen> + </informalexample> + <informalexample> + <programlisting role="php"> +<![CDATA[ +<?php $i = 1; while ($i <= 10): - echo $i; + echo $i . "\n"; $i++; endwhile; -?> ]]> </programlisting> + &example.outputs; + <screen> +<![CDATA[ +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 +]]> + </screen> </informalexample> </para> </sect1>