Home >Backend Development >PHP Tutorial >Why do Parentheses Change the Semantics of Function Calls in PHP?
Parentheses Altering Semantics of Function Call Result
In PHP, wrapping the result of a function call in parentheses can alter its semantics, enabling expressions that would otherwise trigger errors. Without explicit documentation on this behavior, it can be confusing to understand how it works.
This behavior stems from the Zend Engine's parsing logic. When PHP encounters a non-empty function argument list, it classifies it as one of three possibilities: an expression without a variable, a variable, or a variable passed by reference.
By adding parentheses around the function call, the argument is no longer interpreted as a function call. This changes the opcode used to compile the code, which removes the flag indicating a function call.
Additionally, the Zend Engine allows non-references with a reference count of 1 to be treated as references in certain situations. In the provided example, the array returned by get_array() has a reference count of 1, which satisfies this condition.
Therefore, the parentheses prevent the function call from being detected as such, and the return value is treated as a reference with a count of 1, enabling the expression reset((get_array())) to work as intended.
However, it's important to note that this behavior is considered a bug and should not be relied upon. The EBNF representation of the PHP grammar does not explicitly mention this behavior, and relying on it can lead to unexpected results.
The above is the detailed content of Why do Parentheses Change the Semantics of Function Calls in PHP?. For more information, please follow other related articles on the PHP Chinese website!