The function code takes an argument by reference, that is, it can change its value in the calling code.
Why both give out the same value?
With the second case, everything should be easy. Set the initial value of the variable. Called a function that increased its value. As a result, we have 2 .
In the first case, there is a nested function call. Here, the call func($r) in the same way increases the value by 1. Then the result of the function execution (which is not actually defined, since the function does not have a return ) is passed as an argument to the same function again. But since the call argument is no longer $r (but the undefined result of the first call), then, of course, the value of this variable will not change.
And how to understand the string func (func ($ r))
understand exactly as written, call the function func and its result once again to the function func . That is, formally, if this is easier for you to understand, it will be equivalent to writing
$result = func($r); func($result);
Can a function pass itself as an argument?
again, the result of the execution is transmitted, and not itself. However, the approach with the transfer of references to the function is very common. This is called a callback method (or callback function). The simplest example of passing a function as a parameter is sorting usort() . Example:
$data = [0,1,2,3, 1,2]; usort($data, function($a,$b){ return $a - $b;}) ; //либо для PHP 7 usort($data, function($a, $b){ return $a <=> $b;});
As you can see here, the function reference is passed as a parameter. Moreover, this function is anonymous, i.e. It has no name and is defined right at the place of transmission