Daily PHP Tip – Error Supression Operator

A common problem with PHP is that people write code that uses whats called an  “undefined index”,  basically this means they are calling for an unassigned variable.  In PHP, the error-suppression operator is an “@” sign.    It’s best to give you an example as where to use it.

Improper code:

$colors .= "purple,";
$colors .= "red,green,blue";
echo $colors;

This will return “Notice: Undefined variable: colors”

To suppress this error since you know about it:

@$colors .= "purple,";
$colors .= "red,green,blue";
echo $colors;

This will return “purple,red,green,blue” without a Notice warning.   Using @ is also useful for PHP-GD Image functions for capturing image sizes and resizing.

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.