Why Does Wrapping a Function Call in Parentheses Transform it into an Expression in PHP?

Barbara Streisand
Release: 2024-10-31 07:45:02
Original
410 people have browsed it

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();
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!