5
5
6
6
<sect1 xml : id =" functions.user-defined" >
7
7
<title >User-defined functions</title >
8
-
8
+
9
9
<para >
10
10
A function may be defined using syntax such as the following:
11
11
</para >
@@ -168,14 +168,16 @@ function recursion($a)
168
168
</para >
169
169
170
170
</sect1 >
171
-
171
+
172
172
<sect1 xml : id =" functions.arguments" >
173
- <title >Function arguments</title >
174
-
173
+ <title >Function parameters and arguments</title >
174
+
175
175
<simpara >
176
+ The function parameters are declared in the function signature.
176
177
Information may be passed to functions via the argument list,
177
178
which is a comma-delimited list of expressions. The arguments are
178
- evaluated from left to right, before the function is actually called
179
+ evaluated from left to right and the result is assigned to the parameters of
180
+ the function, before the function is actually called
179
181
(<emphasis >eager</emphasis > evaluation).
180
182
</simpara >
181
183
@@ -204,12 +206,12 @@ function takes_array($input)
204
206
</example >
205
207
</para >
206
208
<para >
207
- As of PHP 8.0.0, the list of function arguments may include a trailing comma, which
208
- will be ignored. That is particularly useful in cases where the list of arguments is
209
- long or contains long variable names, making it convenient to list arguments vertically.
209
+ As of PHP 8.0.0, the list of function parameters may include a trailing comma, which
210
+ will be ignored. That is particularly useful in cases where the list of parameters is
211
+ long or contains long variable names, making it convenient to list parameters vertically.
210
212
</para >
211
213
<example >
212
- <title >Function Argument List with trailing Comma </title >
214
+ <title >Function parameter list with trailing comma </title >
213
215
<programlisting role =" php" >
214
216
<![CDATA[
215
217
<?php
@@ -230,7 +232,7 @@ function takes_many_args(
230
232
231
233
<sect2 xml : id =" functions.arguments.by-reference" >
232
234
<title >Passing arguments by reference</title >
233
-
235
+
234
236
<simpara >
235
237
By default, function arguments are passed by value (so that if
236
238
the value of the argument within the function is changed, it does
@@ -239,11 +241,11 @@ function takes_many_args(
239
241
</simpara >
240
242
<para >
241
243
To have an argument to a function always passed by reference, prepend an
242
- ampersand (& ) to the argument name in the function definition:
244
+ ampersand (& ) to the parameter name in the function definition:
243
245
</para >
244
246
<para >
245
247
<example >
246
- <title >Passing function parameters by reference</title >
248
+ <title >Passing function arguments by reference</title >
247
249
<programlisting role =" php" >
248
250
<![CDATA[
249
251
<?php
@@ -260,16 +262,16 @@ echo $str; // outputs 'This is a string, and something extra.'
260
262
</example >
261
263
</para >
262
264
<para >
263
- It is an error to pass a value as argument which is supposed to be passed by reference.
265
+ It is an error to pass a constant expression as argument to parameter that expects to be passed by reference.
264
266
</para >
265
267
</sect2 >
266
268
<sect2 xml : id =" functions.arguments.default" >
267
- <title >Default argument values</title >
268
-
269
+ <title >Default parameter values</title >
270
+
269
271
<para >
270
- A function may define default values for arguments using syntax similar
271
- to assigning a variable. The default is used only when the parameter is
272
- not specified; in particular, note that passing &null; does <emphasis >not</emphasis >
272
+ A function may define default values for parameters using syntax similar
273
+ to assigning a variable. The default is used only when the parameter's argument is
274
+ not passed. Note that passing &null; does <emphasis >not</emphasis >
273
275
assign the default value.
274
276
</para >
275
277
<para >
@@ -367,21 +369,21 @@ Crafting a beautiful coffee just for you.
367
369
example) a variable, a class member or a function call.
368
370
</simpara >
369
371
<para >
370
- Note that any optional arguments should be specified after any
371
- required arguments , otherwise they cannot be omitted from calls.
372
+ Note that any optional parameters should be specified after any
373
+ required parameters , otherwise they cannot be omitted from calls.
372
374
Consider the following example:
373
375
</para >
374
376
<para >
375
377
<example >
376
- <title >Incorrect usage of default function arguments </title >
378
+ <title >Incorrect usage of default function parameters </title >
377
379
<programlisting role =" php" >
378
380
<![CDATA[
379
381
<?php
380
382
function makeyogurt($container = "bowl", $flavour)
381
383
{
382
384
return "Making a $container of $flavour yogurt.\n";
383
385
}
384
-
386
+
385
387
echo makeyogurt("raspberry"); // "raspberry" is $container, not $flavour
386
388
?>
387
389
]]>
@@ -400,15 +402,15 @@ Fatal error: Uncaught ArgumentCountError: Too few arguments
400
402
</para >
401
403
<para >
402
404
<example >
403
- <title >Correct usage of default function arguments </title >
405
+ <title >Correct usage of default function parameters </title >
404
406
<programlisting role =" php" >
405
407
<![CDATA[
406
408
<?php
407
409
function makeyogurt($flavour, $container = "bowl")
408
410
{
409
411
return "Making a $container of $flavour yogurt.\n";
410
412
}
411
-
413
+
412
414
echo makeyogurt("raspberry"); // "raspberry" is $flavour
413
415
?>
414
416
]]>
@@ -427,7 +429,7 @@ Making a bowl of raspberry yogurt.
427
429
</para >
428
430
<para >
429
431
<example >
430
- <title >Correct usage of default function arguments </title >
432
+ <title >Correct usage of default function parameters </title >
431
433
<programlisting role =" php" >
432
434
<![CDATA[
433
435
<?php
@@ -449,16 +451,16 @@ Making a bowl of raspberry natural yogurt.
449
451
</example >
450
452
</para >
451
453
<para >
452
- As of PHP 8.0.0, declaring mandatory arguments after optional arguments
454
+ As of PHP 8.0.0, declaring mandatory parameters after optional parameters
453
455
is <emphasis >deprecated</emphasis >. This can generally be resolved by
454
456
dropping the default value, since it will never be used.
455
- One exception to this rule are arguments of the form
457
+ One exception to this rule are parameters of the form
456
458
<code >Type $param = null</code >, where the &null; default makes the type implicitly
457
459
nullable. This usage is deprecated as of PHP 8.4.0, and an explicit
458
460
<link linkend =" language.types.declarations.nullable" >nullable type</link >
459
461
should be used instead.
460
462
<example >
461
- <title >Declaring optional arguments after mandatory arguments </title >
463
+ <title >Declaring optional parameters after mandatory parameters </title >
462
464
<programlisting role =" php" >
463
465
<![CDATA[
464
466
<?php
@@ -486,7 +488,7 @@ function bar(?A $a, $b) {} // Recommended
486
488
</note >
487
489
<note >
488
490
<simpara >
489
- Arguments that are passed by reference may have a default value.
491
+ Parameters that expect the argument by reference may have a default value.
490
492
</simpara >
491
493
</note >
492
494
</sect2 >
@@ -501,7 +503,7 @@ function bar(?A $a, $b) {} // Recommended
501
503
</simpara >
502
504
503
505
<para >
504
- Argument lists may include the
506
+ Parameter lists may include the
505
507
<literal >...</literal > token to denote that the function accepts a
506
508
variable number of arguments. The arguments will be passed into the
507
509
given variable as an &array; :
@@ -564,7 +566,7 @@ echo add(...$a);
564
566
</para >
565
567
566
568
<para >
567
- You may specify normal positional arguments before the
569
+ You may specify normal positional parameters before the
568
570
<literal >...</literal > token. In this case, only the trailing arguments
569
571
that don't match a positional argument will be added to the array
570
572
generated by <literal >...</literal >.
@@ -701,20 +703,24 @@ htmlspecialchars($string, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, 'UTF-8', fa
701
703
</example >
702
704
703
705
<para >
704
- Passing the same parameter multiple times results in an Error exception.
706
+ Passing an argument to the same named parameter multiple times results in an
707
+ <classname >Error</classname > exception.
705
708
</para >
706
709
707
710
<example >
708
- <title >Error thrown when passing the same parameter multiple times</title >
711
+ <title >Error thrown when passing an argument to the same named parameter multiple times</title >
709
712
<programlisting role =" php" >
710
713
<![CDATA[
711
714
<?php
715
+
712
716
function foo($param) { ... }
713
717
714
718
foo(param: 1, param: 2);
715
719
// Error: Named parameter $param overwrites previous argument
720
+
716
721
foo(1, param: 2);
717
722
// Error: Named parameter $param overwrites previous argument
723
+
718
724
?>
719
725
]]>
720
726
</programlisting >
@@ -745,10 +751,10 @@ var_dump(foo(...[1, 2], b: 20)); // Fatal error. Named parameter $b overwrites p
745
751
746
752
</sect2 >
747
753
</sect1 >
748
-
754
+
749
755
<sect1 xml : id =" functions.returning-values" >
750
756
<title >Returning values</title >
751
-
757
+
752
758
<para >
753
759
Values are returned by using the optional return statement. Any
754
760
type may be returned, including arrays and objects. This causes the
@@ -1463,7 +1469,7 @@ Warning: Cannot bind an instance to a static closure in %s on line %d
1463
1469
<?php
1464
1470
1465
1471
$y = 1;
1466
-
1472
+
1467
1473
$fn1 = fn($x) => $x + $y;
1468
1474
// equivalent to using $y by value:
1469
1475
$fn2 = function ($x) use ($y) {
@@ -1712,7 +1718,7 @@ $obj?->prop->method(...);
1712
1718
</sect1 >
1713
1719
1714
1720
</chapter >
1715
-
1721
+
1716
1722
<!-- Keep this comment at the end of the file
1717
1723
Local variables:
1718
1724
mode: sgml
0 commit comments