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 @@ - + Control Structures 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 @@ + A is equal to 5 ]]> + &example.outputs; + + + @@ -38,6 +46,7 @@ A is equal to 5 ]]> + &example.outputs; + + + @@ -66,12 +80,20 @@ endif; + - // ... + Foo is 1 ]]> + &example.outputs; + + + Whereas this is valid, as the trailing newline after the @@ -82,12 +104,20 @@ endif; + - ... + Foo is 1 ]]> + &example.outputs; + + + 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
\n"; + echo "$val\n"; } - -/* Using the optional argument. */ - +]]> + + &example.outputs; + + + + +
+ + Using the optional argument: + + + + +\n"; + echo "At 5\n"; break 1; /* Exit only the switch. */ case 10: - echo "At 10; quitting
\n"; + echo "At 10; quitting\n"; break 2; /* Exit the switch and the while. */ default: break; } + echo "$i\n"; } -?> ]]>
+ &example.outputs; + + +
diff --git a/language/control-structures/continue.xml b/language/control-structures/continue.xml index 0635a484b60c..cfa2dede274a 100644 --- a/language/control-structures/continue.xml +++ b/language/control-structures/continue.xml @@ -40,7 +40,6 @@ foreach ($arr as $key => $value) { } echo $value . "\n"; } -?> ]]> &examples.outputs; @@ -67,7 +66,6 @@ while ($i++ < 5) { } echo "Neither does this.\n"; } -?> ]]> &examples.outputs; @@ -103,10 +101,9 @@ Inner ]]> 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: - + ]]> @@ -63,7 +62,7 @@ declare(ticks=TICK_VALUE); declare was included then it does not affect the parent file). - + ]]> @@ -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 } - -?> ]]> + &example.outputs; + + + @@ -142,12 +148,11 @@ if ($a > 0) { A script's encoding can be specified per-script using the encoding directive. Declaring an encoding for the script - + ]]> 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); -?> ]]> + &example.outputs; + + +
@@ -50,23 +55,32 @@ do { ]]> + &example.outputs; + + + 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 @@ $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"; } -?> ]]> + &example.outputs; + + + The else statement is only executed if the @@ -53,9 +60,14 @@ if ($a) echo "b"; else echo "c"; -?> ]]> + &example.outputs; + + + Despite the indentation (which does not matter for PHP), the else is associated with the if ($b), 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 @@ $b) { echo "a is bigger than b"; } elseif ($a == $b) { @@ -28,9 +30,14 @@ if ($a > $b) { } else { echo "a is smaller than b"; } -?> ]]> + &example.outputs; + + + @@ -67,6 +74,8 @@ if ($a > $b) { $b): @@ -76,12 +85,21 @@ else if ($a == $b): // Will not compile. endif; ]]> + &example.outputs; + + + $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; - -?> ]]> + &example.outputs; + + +
diff --git a/language/control-structures/for.xml b/language/control-structures/for.xml index f469985bde43..1e3d6001ed30 100644 --- a/language/control-structures/for.xml +++ b/language/control-structures/for.xml @@ -54,38 +54,71 @@ for (expr1; expr2; expr3) + + &example.outputs; + + + + + + + 10) { break; } - echo $i; + echo $i . " "; } - -/* example 3 */ - +]]> + + &example.outputs; + + + + + + + 10) { break; } - echo $i; + echo $i . " "; $i++; } - -/* example 4 */ - -for ($i = 1, $j = 0; $i <= 10; $j += $i, print $i, $i++); -?> ]]> + &example.outputs; + + + + + + + + + &example.outputs; + + + @@ -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); ]]> + &example.outputs.similar; + + + array(2) { + ["name"]=> + string(5) "Kalle" + ["salt"]=> + int(454478) + } + [1]=> + array(2) { + ["name"]=> + string(6) "Pierre" + ["salt"]=> + int(776978) + } +} +]]> + @@ -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); ]]> + &example.outputs.similar; + + + array(2) { + ["name"]=> + string(5) "Kalle" + ["salt"]=> + int(454478) + } + [1]=> + array(2) { + ["name"]=> + string(6) "Pierre" + ["salt"]=> + int(776978) + } +} +]]> + 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) { 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"; } -?> ]]> + &example.outputs; + + 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 +]]> + @@ -139,7 +170,6 @@ foreach ($array as [$a, $b]) { foreach ($array as list($a, $b)) { echo "A: $a; B: $b\n"; } -?> ]]> &example.outputs; @@ -174,7 +204,6 @@ foreach ($array as [, , $c]) { // Skipping over $a and $b echo "$c\n"; } -?> ]]> &example.outputs; @@ -205,7 +234,6 @@ $array = [ foreach ($array as [$a, $b, $c]) { echo "A: $a; B: $b; C: $c\n"; } -?> ]]> &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 -?> ]]> + &example.outputs; + + + int(2) + [1]=> + int(4) + [2]=> + int(6) + [3]=> + &int(8) +} +]]> + @@ -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); ]]> &example.outputs; 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) +} ]]> @@ -290,10 +356,19 @@ foreach ($arr as $key => $value) { ]]> + &example.outputs; + + + 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 @@ ]]> &example.outputs; @@ -59,7 +56,6 @@ Bar ]]> &example.outputs; @@ -88,7 +82,6 @@ j hit 17 ]]> &example.outputs; 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) $b) - echo "a is bigger than b"; -?> + echo "a is bigger than b\n"; ]]> + &example.outputs; + + + @@ -56,13 +63,22 @@ if ($a > $b) $b) { - echo "a is bigger than b"; + echo "a is bigger than b\n"; $b = $a; } -?> +var_dump($b); ]]> + &example.outputs; + + + diff --git a/language/control-structures/include.xml b/language/control-structures/include.xml index 33a91ebc79dc..b9beafae3558 100644 --- a/language/control-structures/include.xml +++ b/language/control-structures/include.xml @@ -54,7 +54,7 @@ Basic <literal>include</literal> example - + Including within functions - + ]]> @@ -140,7 +138,7 @@ echo "A $color $fruit"; // A green <literal>include</literal> through HTTP - + ]]> @@ -200,7 +197,7 @@ include 'http://www.example.com/file.php?foo=1&bar=2'; return value. Comparing return value of include - + ]]> @@ -220,7 +216,7 @@ if ((include 'vars.php') == TRUE) { <literal>include</literal> and the <function>return</function> statement - + Using output buffering to include a PHP file into a string - + ]]> diff --git a/language/control-structures/match.xml b/language/control-structures/match.xml index f74bf7064644..0466a8583a02 100644 --- a/language/control-structures/match.xml +++ b/language/control-structures/match.xml @@ -18,14 +18,13 @@ Structure of a <literal>match</literal> expression - + return_expression, conditional_expression1, conditional_expression2 => return_expression, }; -?> ]]> @@ -43,7 +42,6 @@ $return_value = match ($food) { }; var_dump($return_value); -?> ]]> &example.outputs; @@ -70,7 +68,6 @@ $output = match (true) { }; var_dump($output); -?> ]]> &example.outputs; @@ -135,7 +132,7 @@ string(8) "Teenager" expression will be evaluated. For example: - + baz => beep(), // beep() isn't called unless $x === $this->baz // etc. }; -?> ]]> @@ -157,7 +153,7 @@ $result = match ($x) { - + 5, $c => 5, }; -?> ]]> @@ -178,7 +173,7 @@ $result = match ($x) { This pattern matches anything that wasn't previously matched. For example: - + bar(), default => baz(), }; -?> ]]> @@ -219,7 +213,6 @@ try { } catch (\UnhandledMatchError $e) { var_dump($e); } -?> ]]> &example.outputs; @@ -227,13 +220,13 @@ try { - 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) { ]]> &example.outputs; @@ -296,7 +287,6 @@ $result = match (true) { }; var_dump($result); -?> ]]> &example.outputs; diff --git a/language/control-structures/switch.xml b/language/control-structures/switch.xml index 3ffa3a6098ca..be2fc017cbcd 100644 --- a/language/control-structures/switch.xml +++ b/language/control-structures/switch.xml @@ -38,32 +38,39 @@ ]]> + &example.outputs; + + + @@ -84,17 +91,25 @@ if ($i == 0) { ]]> + &example.outputs; + + + @@ -121,6 +136,8 @@ switch ($i) { ]]> + &example.outputs; + + + @@ -142,6 +164,8 @@ switch ($i) { ]]> + &example.outputs; + + + @@ -203,11 +232,14 @@ switch ($target) { print "D"; break; } - -// Prints "B" -?> ]]> + &example.outputs; + + + @@ -215,7 +247,7 @@ switch ($target) { Or, alternatively, if-else blocks instead of switch. - ]]> + &example.outputs; + + + @@ -250,6 +285,8 @@ switch (true) { ]]> + &example.outputs; + + + - 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.4.0, this syntax is deprecated. ]]> + &example.outputs; + + + 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; + + &example.outputs; + + + + + + + ]]> + &example.outputs; + + +