user712092 user712092 - 1 year ago 83
PHP Question

Order of evaluation of function arguments in PHP

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


Answer Source

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;

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download