For example, if I do this:
function bar(&$var)
{
$foo = function() use ($var)
{
$var++;
};
$foo();
}
$my_var = 0;
bar($my_var);
Will $my_var be modified? If not, how do I get this to work without adding a parameter to $foo?
No, they are not passed by reference – the
usefollows a similar notation like the function’s parameters.As written you achieve that by defining the
useas pass-by-reference:It’s also possible to create recursion this way:
You can validate that on your own with the help of the
debug_zval_dumpfunction (Demo):Output:
A full-through-all-scopes-working reference would have a refcount of 1.