[DOC-CVS] [doc-en] master: WASM: Enable runnable examples for language.functions section (#5463)

[email protected] (AllenJB via GitHub) Wed, 6 May 2026 16:56:44 +0000
Newsgroups php.doc.cvs
Message-ID <[email protected]>
Author: AllenJB (AllenJB)
Committer: GitHub (web-flow)
Pusher: jordikroon
Date: 2026-05-06T18:56:41+02:00

Commit: https://github.com/php/doc-en/commit/9c835d146d452976f21db7d9cb60dca013829d39
Raw diff: https://github.com/php/doc-en/commit/9c835d146d452976f21db7d9cb60dca013829d39.diff

WASM: Enable runnable examples for language.functions section (#5463)

* Enable runnable (WASM) examples for language.functions section

Changed paths:
  M  language/functions.xml


Diff:

diff --git a/language/functions.xml b/language/functions.xml
index d05c88422cc0..52d34c55868c 100644
--- a/language/functions.xml
+++ b/language/functions.xml
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="utf-8"?>
 <!-- $Revision$ -->
- <chapter xml:id="language.functions" xmlns="http://docbook.org/ns/docbook">
+ <chapter xml:id="language.functions" xmlns="http://docbook.org/ns/docbook" annotations="interactive">
   <title>Functions</title>
 
   <sect1 xml:id="functions.user-defined">
@@ -14,7 +14,7 @@
    </para>
    <example>
     <title>Declaring a new function named <literal>foo</literal></title>
-    <programlisting role="php">
+    <programlisting role="php" annotations="non-interactive">
 <![CDATA[
 <?php
 function foo($arg_1, $arg_2, /* ..., */ $arg_n)
@@ -22,7 +22,6 @@ function foo($arg_1, $arg_2, /* ..., */ $arg_n)
     echo "Example function.\n";
     return $retval;
 }
-?>
 ]]>
     </programlisting>
    </example>
@@ -30,11 +29,10 @@ function foo($arg_1, $arg_2, /* ..., */ $arg_n)
     <para>
      As of PHP 8.0.0, the list of parameters may have a trailing comma:
      <informalexample>
-      <programlisting role="php">
+      <programlisting role="php" annotations="non-interactive">
 <![CDATA[
 <?php
 function foo($arg_1, $arg_2,) { }
-?>
 ]]>
       </programlisting>
      </informalexample>
@@ -70,12 +68,16 @@ function foo($arg_1, $arg_2,) { }
      <programlisting role="php">
 <![CDATA[
 <?php
-
 $makefoo = true;
 
 /* We can't call foo() from here 
    since it doesn't exist yet,
    but we can call bar() */
+if (function_exists('foo')) {
+    foo();
+} else {
+    echo "Function foo does not exist (yet).\n";
+}
 
 bar();
 
@@ -95,8 +97,6 @@ function bar()
 {
   echo "I exist immediately upon program start.\n";
 }
-
-?>
 ]]>
      </programlisting>
     </example>
@@ -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();
-
-?>  
 ]]>
      </programlisting>
     </example>
@@ -169,7 +170,8 @@ function recursion($a)
         recursion($a + 1);
     }
 }
-?>
+
+recursion(17);
 ]]>
      </programlisting>
     </example>
@@ -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:
      <informalexample>
-      <programlisting role="php">
+      <programlisting role="php" annotations="non-interactive">
 <![CDATA[
 <?php
 $v = foo(
     $arg_1,
     $arg_2,
 );
-?>
 ]]>
       </programlisting>
      </informalexample>
@@ -232,7 +233,7 @@ $v = foo(
    </para>
    <example>
     <title>Function parameter list with trailing comma</title>
-    <programlisting role="php">
+    <programlisting role="php" annotations="non-interactive">
 <![CDATA[
 <?php
 function takes_many_args(
@@ -245,7 +246,6 @@ function takes_many_args(
 {
     // ...
 }
-?>
 ]]>
     </programlisting>
    </example>
@@ -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.'
-?>
 ]]>
       </programlisting>
      </example>
@@ -308,7 +307,6 @@ function makecoffee($type = "cappuccino")
 echo makecoffee();
 echo makecoffee(null);
 echo makecoffee("espresso");
-?>
 ]]>
       </programlisting>
       &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");
 ]]>
       </programlisting>
       &example.outputs;
@@ -372,7 +370,6 @@ function makecoffee($coffeeMaker = new DefaultCoffeeMaker)
 }
 echo makecoffee();
 echo makecoffee(new FancyCoffeeMaker);
-?>
 ]]>
       </programlisting>
 
@@ -406,14 +403,18 @@ function makeyogurt($container = "bowl", $flavour)
 }
 
 echo makeyogurt("raspberry"); // "raspberry" is $container, not $flavour
-?>
 ]]>
       </programlisting>
       &example.outputs;
       <screen>
 <![CDATA[
-Fatal error: Uncaught ArgumentCountError: Too few arguments
- to function makeyogurt(), 1 passed in example.php on line 42
+Deprecated: makeyogurt(): Optional parameter $container declared before required parameter $flavour is implicitly treated as a required parameter in script on line 2
+
+Fatal error: Uncaught ArgumentCountError: Too few arguments to function makeyogurt(), 1 passed in script on line 7 and exactly 2 expected in script:2
+Stack trace:
+#0 script(7): makeyogurt('raspberry')
+#1 {main}
+  thrown in script on line 2
 ]]>
       </screen>
      </example>
@@ -433,7 +434,6 @@ function makeyogurt($flavour, $container = "bowl")
 }
 
 echo makeyogurt("raspberry"); // "raspberry" is $flavour
-?>
 ]]>
       </programlisting>
       &example.outputs;
@@ -460,7 +460,6 @@ function makeyogurt($container = "bowl", $flavour = "raspberry", $style = "Greek
 }
 
 echo makeyogurt(style: "natural");
-?>
 ]]>
       </programlisting>
       &example.outputs;
@@ -482,10 +481,9 @@ Making a bowl of raspberry natural yogurt.
      should be used instead.
      <example>
       <title>Declaring optional parameters after mandatory parameters</title>
-      <programlisting role="php">
+      <programlisting role="php" annotations="non-interactive">
 <![CDATA[
 <?php
-
 function foo($a = [], $b) {}     // Default not used; deprecated as of PHP 8.0.0
 function foo($a, $b) {}          // Functionally equivalent, no deprecation notice
 
@@ -494,8 +492,6 @@ function bar(A $a = null, $b) {} // As of PHP 8.1.0, $a is implicitly required
                                  // but implicitly nullable (deprecated as of PHP 8.4.0),
                                  // because the default parameter value is null
 function bar(?A $a, $b) {}       // Recommended
-
-?>
 ]]>
       </programlisting>
      </example>
@@ -543,7 +539,6 @@ function sum(...$numbers) {
 }
 
 echo sum(1, 2, 3, 4);
-?>
 ]]>
       </programlisting>
       &example.outputs;
@@ -573,7 +568,6 @@ echo add(...[1, 2])."\n";
 
 $a = [1, 2];
 echo add(...$a);
-?>
 ]]>
       </programlisting>
       &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);
-?>
 ]]>
       </programlisting>
       &example.outputs;
       <screen>
 <![CDATA[
 3 days
-Catchable fatal error: Argument 2 passed to total_intervals() must be an instance of DateInterval, null given, called in - on line 14 and defined in - on line 2
+Fatal error: Uncaught TypeError: total_intervals(): Argument #2 must be of type DateInterval, null given, called in script on line 15 and defined in script:2
+Stack trace:
+#0 script(15): total_intervals('d', NULL)
+#1 {main}
+  thrown in script on line 2
 ]]>
       </screen>
      </example>
@@ -660,7 +657,7 @@ Catchable fatal error: Argument 2 passed to total_intervals() must be an instanc
 
     <example>
      <title>Named argument syntax</title>
-     <programlisting role="php">
+     <programlisting role="php" annotations="non-interactive">
 <![CDATA[
 <?php
 myFunction(paramName: $value);
@@ -668,14 +665,13 @@ array_foobar(array: $value);
 
 // NOT supported.
 function_name($variableStoringParamName: $value);
-?>
 ]]>
      </programlisting>
     </example>
 
     <example>
      <title>Positional arguments versus named arguments</title>
-     <programlisting role="php">
+     <programlisting role="php" annotations="non-interactive">
 <![CDATA[
 <?php
 // Using positional arguments:
@@ -683,7 +679,6 @@ array_fill(0, 100, 50);
 
 // Using named arguments:
 array_fill(start_index: 0, count: 100, value: 50);
-?>
 ]]>
      </programlisting>
     </example>
@@ -694,11 +689,10 @@ array_fill(start_index: 0, count: 100, value: 50);
 
     <example>
      <title>Same example as above with a different order of parameters</title>
-     <programlisting role="php">
+     <programlisting role="php" annotations="non-interactive">
 <![CDATA[
 <?php
 array_fill(value: 50, count: 100, start_index: 0);
-?>
 ]]>
      </programlisting>
     </example>
@@ -712,13 +706,12 @@ array_fill(value: 50, count: 100, start_index: 0);
 
     <example>
      <title>Combining named arguments with positional arguments</title>
-     <programlisting role="php">
+     <programlisting role="php" annotations="non-interactive">
 <![CDATA[
 <?php
 htmlspecialchars($string, double_encode: false);
 // Same as
 htmlspecialchars($string, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, 'UTF-8', false);
-?>
 ]]>
      </programlisting>
     </example>
@@ -730,10 +723,9 @@ htmlspecialchars($string, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, 'UTF-8', fa
 
     <example>
      <title>Error thrown when passing an argument to the same named parameter multiple times</title>
-     <programlisting role="php">
+     <programlisting role="php" annotations="non-interactive">
 <![CDATA[
 <?php
-
 function foo($param) { ... }
 
 foo(param: 1, param: 2);
@@ -741,8 +733,6 @@ foo(param: 1, param: 2);
 
 foo(1, param: 2);
 // Error: Named parameter $param overwrites previous argument
-
-?>
 ]]>
      </programlisting>
     </example>
@@ -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
-?>
 ]]>
      </programlisting>
     </example>
@@ -804,7 +793,6 @@ function square($num)
     return $num * $num;
 }
 echo square(4);   // outputs '16'.
-?>
 ]]>
       </programlisting>
      </example>
@@ -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();
-
-?>
 ]]>
       </programlisting>
      </example>
@@ -843,7 +830,7 @@ list($zero, $one, $two) = small_numbers();
     <para>
      <example>
       <title>Returning a reference from a function</title>
-      <programlisting role="php">
+      <programlisting role="php" annotations="non-interactive">
 <![CDATA[
 <?php
 function &returns_reference()
@@ -852,7 +839,6 @@ function &returns_reference()
 }
 
 $newref =& returns_reference();
-?>
 ]]>
       </programlisting>
      </example>
@@ -889,12 +875,12 @@ $newref =& returns_reference();
 <![CDATA[
 <?php
 function foo() {
-    echo "In foo()<br />\n";
+    echo "In foo()\n";
 }
 
 function bar($arg = '')
 {
-    echo "In bar(); argument was '$arg'.<br />\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()
-?>
 ]]>
      </programlisting>
     </example>
@@ -940,8 +925,6 @@ class Foo
 $foo = new Foo();
 $funcname = "Variable";
 $foo->$funcname();  // This calls $foo->Variable()
-
-?>
 ]]>
      </programlisting>
     </example>
@@ -958,17 +941,22 @@ 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.
-
-?>
 ]]>
      </programlisting>
+     &example.outputs;
+     <screen>
+<![CDATA[
+static property
+Method Variable called
+]]>
+     </screen>
     </example>
    </para>
    <para>
@@ -995,7 +983,6 @@ $func = array(new Foo, "baz");
 $func(); // prints "baz"
 $func = "Foo::bar";
 $func(); // prints "bar"
-?>
 ]]>
      </programlisting>
     </example>
@@ -1088,7 +1075,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)
-?> 
 ]]>
      </programlisting>
     </informalexample>
@@ -1130,7 +1116,6 @@ echo preg_replace_callback('~-([a-z])~', function ($match) {
     return strtoupper($match[1]);
 }, 'hello-world');
 // outputs helloWorld
-?>
 ]]>
     </programlisting>
    </example>
@@ -1154,7 +1139,6 @@ $greet = function($name) {
 
 $greet('World');
 $greet('PHP');
-?>
 ]]>
     </programlisting>
    </example>
@@ -1217,7 +1201,6 @@ $example = function () use ($message): string {
     return "hello $message";
 };
 var_dump($example());
-?>
 ]]>
     </programlisting>
     &example.outputs.similar;
@@ -1303,7 +1286,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
-?>
 ]]>
     </programlisting>
    </example>
@@ -1313,7 +1295,6 @@ print $my_cart->getTotal(0.05) . "\n";
     <programlisting role="php">
 <![CDATA[
 <?php
-
 class Test
 {
     public function testing()
@@ -1327,8 +1308,6 @@ class Test
 $object = new Test;
 $function = $object->testing();
 $function();
-    
-?>
 ]]>
     </programlisting>
     &example.outputs;
@@ -1362,7 +1341,6 @@ object(Test)#1 (0) {
       <programlisting role="php">
 <![CDATA[
 <?php
-
 class Foo
 {
     function __construct()
@@ -1374,15 +1352,17 @@ class Foo
     }
 };
 new Foo();
-
-?>
 ]]>
       </programlisting>
       &example.outputs;
       <screen>
 <![CDATA[
-Notice: Undefined variable: this in %s on line %d
-NULL
+Fatal error: Uncaught Error: Using $this when not in object context in script:7
+Stack trace:
+#0 script(9): Foo::{closure:Foo::__construct():6}()
+#1 script(12): Foo->__construct()
+#2 {main}
+  thrown in script on line 7
 ]]>
       </screen>
      </example>
@@ -1394,20 +1374,22 @@ NULL
       <programlisting role="php">
 <![CDATA[
 <?php
-
 $func = static function() {
     // function body
 };
 $func = $func->bindTo(new stdClass);
 $func();
-
-?>
 ]]>
       </programlisting>
       &example.outputs;
       <screen>
 <![CDATA[
-Warning: Cannot bind an instance to a static closure in %s on line %d
+Warning: Cannot bind an instance to a static closure, this will be an error in PHP 9 in script on line 5
+
+Fatal error: Uncaught Error: Value of type null is not callable in script:6
+Stack trace:
+#0 {main}
+  thrown in script on line 6
 ]]>
       </screen>
      </example>
@@ -1495,7 +1477,6 @@ Warning: Cannot bind an instance to a static closure in %s on line %d
      <programlisting role="php">
 <![CDATA[
 <?php
-
 $y = 1;
 
 $fn1 = fn($x) => $x + $y;
@@ -1505,7 +1486,6 @@ $fn2 = function ($x) use ($y) {
 };
 
 var_export($fn1(3));
-?>
 ]]>
      </programlisting>
      &example.outputs;
@@ -1525,12 +1505,10 @@ var_export($fn1(3));
      <programlisting role="php">
 <![CDATA[
 <?php
-
 $z = 1;
 $fn = fn($x) => fn($y) => $x * $y + $z;
 // Outputs 51
 var_export($fn(5)(10));
-?>
 ]]>
      </programlisting>
     </example>
@@ -1545,18 +1523,15 @@ var_export($fn(5)(10));
    <para>
     <example>
      <title>Examples of arrow functions</title>
-     <programlisting role="php">
+     <programlisting role="php" annotations="non-interactive">
 <![CDATA[
 <?php
-
 fn(array $x) => $x;
 static fn($x): int => $x;
 fn($x = 42) => $x;
 fn(&$x) => $x;
 fn&($x) => $x;
 fn($x, ...$rest) => $rest;
-
-?>
 ]]>
      </programlisting>
     </example>
@@ -1576,13 +1551,10 @@ fn($x, ...$rest) => $rest;
      <programlisting role="php">
 <![CDATA[
 <?php
-
 $x = 1;
 $fn = fn() => $x++; // Has no effect
 $fn();
 var_export($x);  // Outputs 1
-
-?>
 ]]>
      </programlisting>
     </example>
@@ -1637,10 +1609,9 @@ var_export($x);  // Outputs 1
     <code>CallableExpr(...)</code> syntax is used to create a <classname>Closure</classname> object from callable. <code>CallableExpr</code> accepts any expression that can be directly called in the PHP grammar:
     <example>
      <title>Simple first class callable syntax</title>
-     <programlisting role="php">
+     <programlisting role="php" annotations="non-interactive">
 <![CDATA[
 <?php
-
 class Foo {
    public function method() {}
    public static function staticmethod() {}
@@ -1664,7 +1635,6 @@ $f6 = $classStr::$staticmethodStr(...);
 $f7 = 'strlen'(...);
 $f8 = [$obj, 'method'](...);
 $f9 = [Foo::class, 'staticmethod'](...);
-?>
 ]]>
      </programlisting>
     </example>
@@ -1684,7 +1654,6 @@ $f9 = [Foo::class, 'staticmethod'](...);
      <programlisting role="php">
 <![CDATA[
 <?php
-
 class Foo {
     public function getPrivateMethod() {
         return [$this, 'privateMethod'];
@@ -1700,7 +1669,14 @@ $privateMethod = $foo->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.
-
+]]>
+     </programlisting>
+    </example>
+   </para>
+   <informalexample>
+    <programlisting role="php">
+<![CDATA[
+<?php
 class Foo1 {
     public function getPrivateMethod() {
         // Uses the scope where the callable is acquired.
@@ -1715,12 +1691,9 @@ class Foo1 {
 $foo1 = new Foo1;
 $privateMethod = $foo1->getPrivateMethod();
 $privateMethod();  // Foo1::privateMethod
-?>
 ]]>
-     </programlisting>
-    </example>
-
-   </para>
+    </programlisting>
+   </informalexample>
 
    <note>
     <para>
@@ -1732,12 +1705,11 @@ $privateMethod();  // Foo1::privateMethod
     <para>
      The first-class callable syntax cannot be combined with the <link linkend="language.oop5.basic.nullsafe">nullsafe operator</link>. Both of the following result in a compile-time error:
      <informalexample>
-      <programlisting role="php">
+      <programlisting role="php" annotations="non-interactive">
 <![CDATA[
 <?php
 $obj?->method(...);
 $obj?->prop->method(...);
-?>
 ]]>
       </programlisting>
      </informalexample>