Home > Article > Backend Development > Why Does Wrapping a Function Call in Parentheses Transform it into an Expression in PHP?
Parentheses Transforming Function Call Result into Expression
In PHP, wrapping the outcome of a function call in parentheses can surprisingly transform the result into a полноценное выражение, as seen in the following example:
<?php function get_array() { return array(); } function foo() { return reset((get_array())); } foo();
The above code works without error, although there is no clear explanation in the documentation. Let's investigate this behavior in detail.
First Condition: Non-Function Call Argument
Due to the added parentheses, PHP no longer identifies the argument as a function call but as an "expr_without_variable." This change in parsing affects how the code is compiled, ultimately removing the "function call" flag from the "SEND_VAR_NO_REF" opcode.
Second Condition: Reference Count of 1
The Zend Engine occasionally allows non-references with a reference count of 1 where references are expected. In this example, the returned array has a reference count of 1, meeting this condition. However, if the array were referenced from elsewhere, the second condition would not be met, resulting in an error.
Conclusion
While this behavior may seem convenient, it's crucial to note that it is not a documented feature and should be treated as a possible bug. Relying on it can introduce unexpected errors and instability in code. Therefore, it's highly recommended to avoid this syntax and instead use the correct expression to avoid ambiguity and ensure predictable results.
The above is the detailed content of Why Does Wrapping a Function Call in Parentheses Transform it into an Expression in PHP?. For more information, please follow other related articles on the PHP Chinese website!