search
HomeBackend DevelopmentPHP TutorialAnalysis of the bug that the PHP error suppressor (@) causes failure to pass parameters by reference_PHP Tutorial

Look at the example below:

Copy code The code is as follows:

$array = array(1 ,2,3);
function add (&$arr) {
$arr[] = 4;
}
add(@$array);
print_r($array);
/**
At this time, $array has not changed, output:
Array
(
[0] => 1
[1] => 2
[2] => 3
)
*/
add($array);
print_r($array);
/**
Without error suppression, the output is normal:
Array
(
[0] => 1
[1] => 2
[2] => ; 3
[3] => 4
)
*/
?>

I have never encountered this problem before, so I first looked for relevant information to see if there is any ready-made answer. After searching on Google, I found that although someone has reported a similar bug to PHP: http:// bugs.php.net/bug.php?id=47623, but PHP official has not solved it yet and has not given a reply.

There is no way, I can only analyze it myself. I have introduced the principle of error suppression in the article before (In-depth understanding of PHP principles of error suppression and embedded HTML). In principle, error suppression only modifies error_reporting level, it stands to reason that it will not affect the function calling mechanism between contexts. This can only be done through field testing.

After gdb tracking, it was found that after using the wrong transplantation operator, the parameter opcode before the function call is different:

Copy the code The code is as follows:

//When the error suppressor is not used
OPCODE = SEND_REF
//After the error suppressor is used
OPCODE = SEND_VAR_NO_RE

Initial problem Positioned, but what is the reason for this difference?

Since the OPCODE is different, it must have taken different branches during the syntax analysis stage. Thinking of this level, the problem is solved Positioned,

It turns out that in the PHP syntax analysis stage, entries in the form of "@" + expr are reduced to expr_without_variable, and the meaning of this kind of node is the value without a variable, that is, a literal value. We We all know that literal values ​​cannot be passed by reference (because it is not a variable), so this difference will result.

The specific process is as follows:
1. Syntax analysis stage:
Copy code The code is as follows:

expr_without_variable:
//...with omission
| '@' { zend_do_begin_silence(&$1 TSRMLS_CC); }
expr { zend_do_end_silence(&$1 TSRMLS_CC); $$ = $3; }
//The ZEND_SEND_VAL branch is gone here
non_empty_function_call_parameter_list:
expr_without_variable { ....} // I mistakenly took this branch
| variable {..... } //Normal situation

So different OPCODEs are generated during compilation, which also leads to the appearance of problems.
Finally, I have explained the reason on this bug page of PHP. If you are interested, you can check out my poor English. Finally, thank you cici netizen for providing this interesting question.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323224.htmlTechArticleLook at the example below: Copy the code The code is as follows: ?php $array = array(1,2,3); function add ( } add(@$array); print_r($array); /** At this time, $array has not changed, output: Array ( [0] = 1 [1...
Statement
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
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)