Press "Enter" to skip to content

PHP Internals — Error Suppression and Inline HTML

PHP supports inline HTML. So how does PHP handle the HTML that sits outside of PHP tags in a PHP file? Is it any different from outputting HTML directly?
PHP provides an error suppression operator '@'. How does it stop errors from being output? And when should I use it?
These are common questions raised by some readers over the past couple of days, so today I'll just answer them all in one go, for future readers to look up.

How PHP handles inline HTML in a file

In PHP, every character outside of tags is turned into a T_INLINE_HTML token during lexical analysis, and during parsing all T_INLIE_HTML tokens are assigned a ZEND_ECHO output.
In other words:

<?php
while($con) {
?>
laruence
<?php
 }
?>

generates a single OPLINE: T_ECHO, whose operand is "laruence";
As far as the result goes, the code above is actually the same as the following:

<?php
 while($con) {
   echo "laruence";
 }
?>

But there is one thing to watch out for: characters outside of PHP tags are split into 400-character units during lexical analysis. For example:

<?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 code above there are 531 characters outside the tags (including spaces and newlines), and they will be split into two T_INLINE_HTML outputs.

Error suppression operator

As we know, in PHP you can silence error messages with the error suppression operator. So how does it actually do that?
During parsing, for:

<?php
	@include('file');
?>

two Oplines (operations) are inserted before and after the include statement, respectively. These two operations do the following:

1. Save the current error_reporting value, and set error_reporting(0); // turn off error output
2. Restore the previously saved error_reporting value.

In other words, the code above is actually similar to the following:

$old = error_reporting(0);
include('file');
error_reporting($old);

On a side note, let me say one more thing: "when should error suppression be used?" My personal advice is that if an error on this statement doesn't affect you much, you don't care what the error is, and you aren't going to write extra logic to handle it, then you can use error suppression. Otherwise, please use extra logic to detect the error.

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.