update page now
Laravel Live Japan

Voting

: max(two, five)?
(Example: nine)

The Note You're Voting On

Luna
2 years ago
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.

<< Back to user notes page

To Top