Deeply understand the error suppression and embedded HTML analysis of PHP principle

  • 2020-05-05 11:03:04
  • OfStack

PHP provides an error suppressor '@'. How does it prevent error output? And when should I use it?
This is the two days some netizens mentioned the common question, today on the overall answer, ready to read The PHP file is embedded with HTML for processing
In PHP, all characters outside the label are translated into T_INLINE_HTML token during lexical analysis, and all T_INLIE_HTML are assigned ZEND_ECHO output.
In other words:
 
<?php 
while($con) { 
?> 
laruence 
<?php 
} 
?> 

An OPLINE: T_ECHO is generated, and the operand is "laruence".
In terms of results, the above code is the same as the following:
 
<?php 
while($con) { 
echo "laruence"; 
} 
?> 

One thing to note, however, is that for characters outside the PHP tag, the lexical analysis process will be divided into 400 characters, such as
 
<?php 
if(1) { 
?> 
laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence 
<?php 
} 
?> 

In the above code, there are 531 characters (including a space carriage return) outside the label, which will be divided into two lines T_INLINE_HTML output.
Error suppressor
We know that in PHP you can silence an error message with an error suppressor, so how does that work?
In the process of parsing, for:
 
<?php 
@include('file'); 
?> 

Two Opline(operations) are inserted before and after the include statement, respectively. The two operations do
 
1.  Save the current error_reporting value ,  And set the error_reporting(0); // Turn off error output  
2.  Restore what was saved before error_reporting value  

In other words, the code above is similar to the code below:
 
$old = error_reporting(0); 
include('file'); 
error_reporting($old); 

Also, as an aside: "when is error suppression applied?" My personal suggestion is that if this statement goes wrong and it doesn't really matter to you, you don't care what the error is, and you don't arrange extra logic to handle it, you can use error suppression.

Related articles: