From 31b59bc72bdffe951ecaef66193469f0e51f2f47 Mon Sep 17 00:00:00 2001 From: AllenJB Date: Sun, 5 Apr 2026 13:22:28 +0100 Subject: [PATCH 1/2] Enable runnable (WASM) examples for language.functions section --- language/functions.xml | 162 +++++++++++++++++------------------------ 1 file changed, 65 insertions(+), 97 deletions(-) diff --git a/language/functions.xml b/language/functions.xml index 5188ab626c7d..e57f69a98df4 100644 --- a/language/functions.xml +++ b/language/functions.xml @@ -1,6 +1,6 @@ - + Functions @@ -14,7 +14,7 @@ Declaring a new function named <literal>foo</literal> - + ]]> @@ -30,11 +29,10 @@ function foo($arg_1, $arg_2, /* ..., */ $arg_n) As of PHP 8.0.0, the list of parameters may have a trailing comma: - + ]]> @@ -70,12 +68,16 @@ function foo($arg_1, $arg_2,) { } ]]> @@ -117,6 +117,9 @@ function foo() /* We can't call bar() yet since it doesn't exist. */ +if (function_exists('bar')) { + bar(); +} foo(); @@ -125,8 +128,6 @@ foo(); made it accessible. */ bar(); - -?> ]]> @@ -169,7 +170,8 @@ function recursion($a) recursion($a + 1); } } -?> + +recursion(17); ]]> @@ -211,14 +213,13 @@ function recursion($a) As of PHP 7.3.0, it is possible to have a trailing comma in the argument list for a function calls: - + ]]> @@ -232,7 +233,7 @@ $v = foo( Function parameter list with trailing comma - + ]]> @@ -276,7 +276,6 @@ function add_some_extra(&$string) $str = 'This is a string, '; add_some_extra($str); echo $str; // outputs 'This is a string, and something extra.' -?> ]]> @@ -308,7 +307,6 @@ function makecoffee($type = "cappuccino") echo makecoffee(); echo makecoffee(null); echo makecoffee("espresso"); -?> ]]> &example.outputs; @@ -338,7 +336,7 @@ function makecoffee($types = array("cappuccino"), $coffeeMaker = NULL) return "Making a cup of ".join(", ", $types)." with $device.\n"; } echo makecoffee(); -echo makecoffee(array("cappuccino", "lavazza"), "teapot");?> +echo makecoffee(array("cappuccino", "lavazza"), "teapot"); ]]> &example.outputs; @@ -372,7 +370,6 @@ function makecoffee($coffeeMaker = new DefaultCoffeeMaker) } echo makecoffee(); echo makecoffee(new FancyCoffeeMaker); -?> ]]> @@ -406,14 +403,18 @@ function makeyogurt($container = "bowl", $flavour) } echo makeyogurt("raspberry"); // "raspberry" is $container, not $flavour -?> ]]> &example.outputs; @@ -433,7 +434,6 @@ function makeyogurt($flavour, $container = "bowl") } echo makeyogurt("raspberry"); // "raspberry" is $flavour -?> ]]> &example.outputs; @@ -460,7 +460,6 @@ function makeyogurt($container = "bowl", $flavour = "raspberry", $style = "Greek } echo makeyogurt(style: "natural"); -?> ]]> &example.outputs; @@ -482,10 +481,9 @@ Making a bowl of raspberry natural yogurt. should be used instead. Declaring optional parameters after mandatory parameters - + ]]> @@ -543,7 +539,6 @@ function sum(...$numbers) { } echo sum(1, 2, 3, 4); -?> ]]> &example.outputs; @@ -573,7 +568,6 @@ echo add(...[1, 2])."\n"; $a = [1, 2]; echo add(...$a); -?> ]]> &example.outputs; @@ -618,14 +612,17 @@ echo total_intervals('d', $a, $b).' days'; // This will fail, since null isn't a DateInterval object. echo total_intervals('d', null); -?> ]]> &example.outputs; @@ -660,7 +657,7 @@ Catchable fatal error: Argument 2 passed to total_intervals() must be an instanc Named argument syntax - + ]]> Positional arguments versus named arguments - + ]]> @@ -694,11 +689,10 @@ array_fill(start_index: 0, count: 100, value: 50); Same example as above with a different order of parameters - + ]]> @@ -712,13 +706,12 @@ array_fill(value: 50, count: 100, start_index: 0); Combining named arguments with positional arguments - + ]]> @@ -730,10 +723,9 @@ htmlspecialchars($string, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, 'UTF-8', fa Error thrown when passing an argument to the same named parameter multiple times - + ]]> @@ -765,7 +755,6 @@ var_dump(foo(...[1, 2], d: 40)); // 46 var_dump(foo(...['b' => 2, 'a' => 1], d: 40)); // 46 var_dump(foo(...[1, 2], b: 20)); // Fatal error. Named parameter $b overwrites previous argument -?> ]]> @@ -804,7 +793,6 @@ function square($num) return $num * $num; } echo square(4); // outputs '16'. -?> ]]> @@ -826,11 +814,10 @@ function small_numbers() } // Array destructuring will collect each member of the array individually [$zero, $one, $two] = small_numbers(); +var_dump($zero, $one, $two); // Prior to 7.1.0, the only equivalent alternative is using list() construct list($zero, $one, $two) = small_numbers(); - -?> ]]> @@ -843,7 +830,7 @@ list($zero, $one, $two) = small_numbers(); Returning a reference from a function - + ]]> @@ -889,12 +875,12 @@ $newref =& returns_reference(); \n"; + echo "In foo()\n"; } function bar($arg = '') { - echo "In bar(); argument was '$arg'.
\n"; + echo "In bar(); argument was '$arg'.\n"; } // This is a wrapper function around echo @@ -911,7 +897,6 @@ $func('test'); // This calls bar() $func = 'echoit'; $func('test'); // This calls echoit() -?> ]]> @@ -940,8 +925,6 @@ class Foo $foo = new Foo(); $funcname = "Variable"; $foo->$funcname(); // This calls $foo->Variable() - -?> ]]> @@ -958,15 +941,13 @@ class Foo static $variable = 'static property'; static function Variable() { - echo 'Method Variable called'; + echo "Method Variable called\n"; } } -echo Foo::$variable; // This prints 'static property'. It does need a $variable in this scope. +echo Foo::$variable ."\n"; // This prints 'static property'. It does need a $variable in this scope. $variable = "Variable"; Foo::$variable(); // This calls $foo->Variable() reading $variable in this scope. - -?> ]]> @@ -995,7 +976,6 @@ $func = array(new Foo, "baz"); $func(); // prints "baz" $func = "Foo::bar"; $func(); // prints "bar" -?> ]]> @@ -1088,7 +1068,6 @@ var_dump(strlen(null)); var_dump(str_contains("foobar", null)); // "Deprecated: Passing null to parameter #2 ($needle) of type string is deprecated" as of PHP 8.1.0 // bool(true) -?> ]]> @@ -1130,7 +1109,6 @@ echo preg_replace_callback('~-([a-z])~', function ($match) { return strtoupper($match[1]); }, 'hello-world'); // outputs helloWorld -?> ]]> @@ -1154,7 +1132,6 @@ $greet = function($name) { $greet('World'); $greet('PHP'); -?> ]]> @@ -1217,7 +1194,6 @@ $example = function () use ($message): string { return "hello $message"; }; var_dump($example()); -?> ]]> &example.outputs.similar; @@ -1303,7 +1279,6 @@ $my_cart->add('eggs', 6); // Print the total with a 5% sales tax. print $my_cart->getTotal(0.05) . "\n"; // The result is 54.29 -?> ]]> @@ -1313,7 +1288,6 @@ print $my_cart->getTotal(0.05) . "\n"; testing(); $function(); - -?> ]]> &example.outputs; @@ -1362,7 +1334,6 @@ object(Test)#1 (0) { ]]> &example.outputs; __construct() +#2 {main} + thrown in script on line 7 ]]> @@ -1394,20 +1367,22 @@ NULL bindTo(new stdClass); $func(); - -?> ]]> &example.outputs; @@ -1495,7 +1470,6 @@ Warning: Cannot bind an instance to a static closure in %s on line %d $x + $y; @@ -1505,7 +1479,6 @@ $fn2 = function ($x) use ($y) { }; var_export($fn1(3)); -?> ]]> &example.outputs; @@ -1525,12 +1498,10 @@ var_export($fn1(3)); fn($y) => $x * $y + $z; // Outputs 51 var_export($fn(5)(10)); -?> ]]> @@ -1545,18 +1516,15 @@ var_export($fn(5)(10)); Examples of arrow functions - + $x; static fn($x): int => $x; fn($x = 42) => $x; fn(&$x) => $x; fn&($x) => $x; fn($x, ...$rest) => $rest; - -?> ]]> @@ -1576,13 +1544,10 @@ fn($x, ...$rest) => $rest; $x++; // Has no effect $fn(); var_export($x); // Outputs 1 - -?> ]]> @@ -1637,10 +1602,9 @@ var_export($x); // Outputs 1 CallableExpr(...) syntax is used to create a Closure object from callable. CallableExpr accepts any expression that can be directly called in the PHP grammar: Simple first class callable syntax - + ]]> @@ -1684,7 +1647,6 @@ $f9 = [Foo::class, 'staticmethod'](...); getPrivateMethod(); $privateMethod(); // Fatal error: Call to private method Foo::privateMethod() from global scope // This is because call is performed outside from Foo and visibility will be checked from this point. - +]]> + + + + + + +getPrivateMethod(); $privateMethod(); // Foo1::privateMethod -?> ]]> - + @@ -1732,12 +1701,11 @@ $privateMethod(); // Foo1::privateMethod The first-class callable syntax cannot be combined with the nullsafe operator. Both of the following result in a compile-time error: - + method(...); $obj?->prop->method(...); -?> ]]> From 39ff4bf355c9dddc9b52b223aee0d7e5278e57b7 Mon Sep 17 00:00:00 2001 From: AllenJB Date: Sun, 26 Apr 2026 11:45:43 +0100 Subject: [PATCH 2/2] Enable runnable (WASM) examples for language.functions section (review amends) --- language/functions.xml | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/language/functions.xml b/language/functions.xml index e57f69a98df4..ecc7aeb8ea09 100644 --- a/language/functions.xml +++ b/language/functions.xml @@ -950,6 +950,13 @@ $variable = "Variable"; Foo::$variable(); // This calls $foo->Variable() reading $variable in this scope. ]]> + &example.outputs; + + + @@ -1666,9 +1673,8 @@ $privateMethod(); - - - + + getPrivateMethod(); $privateMethod(); // Foo1::privateMethod ]]> - - - - + +