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..ab0d7134ddf0 100644 --- a/language/control-structures/alternative-syntax.xml +++ b/language/control-structures/alternative-syntax.xml @@ -17,7 +17,9 @@ + A is equal to 5 ]]> @@ -38,6 +40,7 @@ A is equal to 5 ]]> @@ -66,9 +68,11 @@ endif; + - // ... + Foo is 1 ]]> @@ -82,9 +86,11 @@ endif; + - ... + Foo is 1 ]]> diff --git a/language/control-structures/break.xml b/language/control-structures/break.xml index 4e1b7f2fb23c..5d53aee82b6d 100644 --- a/language/control-structures/break.xml +++ b/language/control-structures/break.xml @@ -26,25 +26,24 @@ 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. */ - +echo "\nUsing the optional argument:\n"; $i = 0; while (++$i) { switch ($i) { case 5: - echo "At 5
\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"; } -?> ]]>
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..94766aaa1f2f 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,10 +121,8 @@ $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 } - -?> ]]> @@ -142,12 +138,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..f1dcad9ecee3 100644 --- a/language/control-structures/do-while.xml +++ b/language/control-structures/do-while.xml @@ -28,7 +28,6 @@ $i = 0; do { echo $i; } while ($i > 0); -?> ]]>
@@ -50,21 +49,24 @@ do { ]]> diff --git a/language/control-structures/else.xml b/language/control-structures/else.xml index dfe7db2b4f41..75dae87a8753 100644 --- a/language/control-structures/else.xml +++ b/language/control-structures/else.xml @@ -19,12 +19,13 @@ $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"; } -?> ]]> @@ -53,7 +54,6 @@ if ($a) echo "b"; else echo "c"; -?> ]]> diff --git a/language/control-structures/elseif.xml b/language/control-structures/elseif.xml index 5685c704d3de..2b55bca85edd 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,7 +30,6 @@ if ($a > $b) { } else { echo "a is smaller than b"; } -?> ]]> @@ -67,6 +68,8 @@ if ($a > $b) { $b): @@ -82,6 +85,9 @@ endif; $b): echo $a." is greater than ".$b; @@ -90,8 +96,6 @@ elseif ($a == $b): // Note the combination of the words. else: echo $a." is neither greater than or equal to ".$b; endif; - -?> ]]> diff --git a/language/control-structures/for.xml b/language/control-structures/for.xml index f469985bde43..1d48a6a022de 100644 --- a/language/control-structures/for.xml +++ b/language/control-structures/for.xml @@ -54,36 +54,31 @@ for (expr1; expr2; expr3) 10) { break; } - echo $i; + echo $i . " "; } -/* example 3 */ - +echo "\nExample 3:\n"; $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++); -?> +echo "\nExample 4:\n"; +for ($i = 1, $j = 0; $i <= 10; $j += $i, print $i . " ", $i++); ]]> @@ -127,9 +122,9 @@ $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); ]]> @@ -151,9 +146,9 @@ $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); ]]> diff --git a/language/control-structures/foreach.xml b/language/control-structures/foreach.xml index 8cffaca5fa24..714975c851c2 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,11 +82,10 @@ foreach ($grid as $y => $row) { } } -/* Example: dynamic arrays */ +echo "\n\nDynamic arrays:\n"; foreach (range(1, 5) as $value) { echo "$value\n"; } -?> ]]> @@ -139,7 +138,6 @@ foreach ($array as [$a, $b]) { foreach ($array as list($a, $b)) { echo "A: $a; B: $b\n"; } -?> ]]> &example.outputs; @@ -174,7 +172,6 @@ foreach ($array as [, , $c]) { // Skipping over $a and $b echo "$c\n"; } -?> ]]> &example.outputs; @@ -205,7 +202,6 @@ $array = [ foreach ($array as [$a, $b, $c]) { echo "A: $a; B: $b; C: $c\n"; } -?> ]]> &example.outputs; @@ -238,8 +234,8 @@ 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 -?> ]]> @@ -260,25 +256,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,8 +309,8 @@ foreach ($arr as $key => $value) { ]]> 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..15c06704c4f9 100644 --- a/language/control-structures/if.xml +++ b/language/control-structures/if.xml @@ -36,9 +36,10 @@ if (expr) $b) - echo "a is bigger than b"; -?> + echo "a is bigger than b\n"; ]]> @@ -56,11 +57,13 @@ if ($a > $b) $b) { - echo "a is bigger than b"; + echo "a is bigger than b\n"; $b = $a; } -?> +var_dump($b); ]]> 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..f39c96800a9f 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(33) "Unhandled match case 5" ["string":"Error":private]=> string(0) "" ["code":protected]=> int(0) ["file":protected]=> - string(9) "/in/ICgGK" + string(9) "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..829c889b674c 100644 --- a/language/control-structures/switch.xml +++ b/language/control-structures/switch.xml @@ -38,30 +38,30 @@ ]]> @@ -84,15 +84,16 @@ if ($i == 0) { ]]> @@ -121,6 +122,8 @@ switch ($i) { ]]> @@ -142,6 +144,8 @@ switch ($i) { ]]> @@ -205,7 +208,6 @@ switch ($target) { } // Prints "B" -?> ]]> @@ -236,7 +238,6 @@ switch (true) { } // Prints "B" -?> ]]> @@ -250,6 +251,8 @@ switch (true) { ]]> - 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. This syntax is deprecated. ]]> diff --git a/language/control-structures/while.xml b/language/control-structures/while.xml index 7551356875b9..bbc740ffe8ce 100644 --- a/language/control-structures/while.xml +++ b/language/control-structures/while.xml @@ -57,19 +57,18 @@ endwhile; $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 */ $i = 1; while ($i <= 10): - echo $i; + echo $i . "\n"; $i++; endwhile; -?> ]]>