note 128022 added to functions.arguments
| From: | Luna at main dot php dot net | Date: | Thu, 15 Dec 2022 18:23:51 +0000 |
| Subject: | note 128022 added to functions.arguments | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-219978@lists.php.net to get a copy of this message | ||
When using named arguments and adding default values only to some of the arguments, the arguments
with default values must be specified at the end or otherwise PHP throws an error:
<?php
function test1($a, $c, $b = 2)
{
return $a + $b + $c;
}
function test2($a, $b = 2, $c)
{
return $a + $b + $c;
}
echo test1(a: 1, c: 3)."\n"; // Works
echo test2(a: 1, c: 3)."\n"; // ArgumentCountError: Argument #2 ($b) not passed
?>
I assume that this happens because internally PHP rewrites the calls to something like test1(1, 3)
and test2(1, , 3). The first call is valid, but the second obviously isn't.
----
Server IP: 45.112.84.4
Probable Submitter: 185.85.3.175 (proxied: 185.41.76.163)
----
Manual Page -- https://php.net/manual/en/functions.arguments.php
Edit -- https://main.php.net/note/edit/128022
Del: integrated -- https://main.php.net/note/delete/128022/integrated
Del: useless -- https://main.php.net/note/delete/128022/useless
Del: bad code -- https://main.php.net/note/delete/128022/bad+code
Del: spam -- https://main.php.net/note/delete/128022/spam
Del: non-english -- https://main.php.net/note/delete/128022/non-english
Del: in docs -- https://main.php.net/note/delete/128022/in+docs
Del: other reasons-- https://main.php.net/note/delete/128022
Reject -- https://main.php.net/note/reject/128022
Search -- https://main.php.net/manage/user-notes.php