PHP error Suppressor (@) analysis _php techniques that cause reference failure bugs

Source: Internet
Author: User
Tags php error
Look at the following example:
Copy Code code as follows:

<?php
$array = Array (1,2,3);
function Add (& $arr) {
$arr [] = 4;
}
Add (@ $array);
Print_r ($array);
/**
At this point, the $array has not changed, the output:
Array
(
[0] => 1
[1] => 2
[2] => 3
)
*/
Add ($array);
Print_r ($array);
/**
Output is normal without using error suppression:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
*/
?>

This problem, I have not encountered before, so first to find relevant information to see if there is no ready-made answer, Goolge, found that although someone has reported to PHP similar bug:http://bugs.php.net/bug.php?id=47623, But the PHP authorities have not resolved, nor given a reply.

No way, I can only analyze, before I have in the article introduced the principle of error suppressor (in-depth understanding of the PHP principle of error suppression and embedded HTML), in principle, the error suppression is only modified error_reporting level, The mechanism of function calls between contexts is not logically affected. Only through field trials.

After the GDB trace, it is found that after using the error-porting character, the opcode of the function call is different:

Copy Code code as follows:

When the error suppressor is not used
OPCODE = Send_ref
After using the error suppression symbol
OPCODE = Send_var_no_re

The problem is initially fixed, but what is the cause of this discrepancy?

Since opcode is different, then certainly is in the grammar analysis stage, has walked the different branch, thought this layer, the question also good localization,

Originally, the PHP parsing phase, the form like "@" +expr, the statute became expr_without_variable, and the meaning of this node is not the value of variables, that is, literals, we all know that the literal value is not to pass the reference (because it is not a variable), so, Can lead to this difference.

The specific process is as follows:
1. Grammar Analysis phase:
Copy Code code as follows:

Expr_without_variable:
//... Have omitted
| ' @ ' {zend_do_begin_silence (&$1 tsrmls_cc);}
Expr {zend_do_end_silence (&$1 tsrmls_cc); $$ = $;}
Here goes the Zend_send_val branch
Non_empty_function_call_parameter_list:
expr_without_variable {...}//Wrong Walk this branch
| Variable {...}//Normal condition

As a result, different opcode are generated during compilation, which also leads to the appearance of the problem.
Finally, I have explained the reason in this bug page of PHP, interested can go to see my bad English level. Finally, thank CiCi netizens for this interesting question.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.