PHP default function arguments - not what you expect
Here's another thing you might want to watch out for in PHP (if you're used to Python or UserTalk, and probably most other languages). It doesn't seem that it lets you address arguments by name, only by position. That is, it lets you address them by name but ignores the name and uses the position instead.
Example:
error_reporting(E_STRICT | E_ALL);
function test_default_args($a = 1, $b = 2, $c = 3)
{
echo "a $a, b $b, c $c<br>";
}
test_default_args($b = 123);
This prints:
a 123, b 2, c 3
So even though you told it $b = 123
, it assigned that to $a
instead. I guess it's just evaluating the expression ($b = 123)
, which creates a local var called $b
, assigns 123
to it, and returns 123
.
It would be nice if there was a warning for this, like C's possibly incorrect assignment
. It doesn't look like PHP checks for that case - assignments not inside an explicit comparison like (($a = foo()) != NULL)
.