Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order of evaluation of function arguments in PHP

Is order of evaluation of PHP function arguments guaranteed to be always the same?

Thanks.

like image 406
user712092 Avatar asked Dec 15 '22 04:12

user712092


2 Answers

Usually, yes. As the manual states:

[Function] arguments are evaluated from left to right.

But there are two edge cases where arguments are not evaluated at all:

Undefined functions

$calls = 0;
register_shutdown_function(function () use (&$calls) {
    echo $calls;
});
func_does_not_exist($calls++);

This outputs 0 on all versions of PHP.

Missing constructor, a special case of an undefined function

class Foo {}

$bar = 0;
$foo = new Foo($bar++);
echo $bar;

This outputs 0 on PHP < 7.1, and 1 on PHP >= 7.1. It's been called the "Rasmus optimization", and it occurs only in the case of constructing classes without formal constructors. See also #67829, #54162 and #54170.


In summary, the manual is correct. For defined functions, arguments are evaluated left-to-right then passed into the function. Undefined functions, for which a non-existent constructor is a special case, do not qualify as functions and so the evaluation before calling is itself undefined.

like image 72
bishop Avatar answered Dec 25 '22 11:12

bishop


From the manual:

Information may be passed to functions via the argument list, which is a comma-delimited list of expressions. The arguments are evaluated from left to right.

In theory it might change in future versions of PHP, but I certainly wouldn't expect it to.

(And please don't write any code that relies on it, for everyone's sake...)

like image 45
iainn Avatar answered Dec 25 '22 10:12

iainn