PHP supports one error control operator: the at sign (@). When prepended to an expression any error generated by that expression will be ignored. It can also be useful for hiding errors generated by various functions.Take the following simple example:
$var = $_GET['data'];
If the ‘data’ parameter is not defined the expression will generate an error.
Notice: Undefined index: data in /var/www/test.php on line 9
You can hide the error using the silence @-operator.
$var = @ $_GET['data'];
Although quite useful at some times, using the @-operator can have some annoying side effects. Say you are using some external libraries in your application which uses the @-operator. If everything works fine than good. But if the library is generating some errors than it becomes difficult to point the exact location where the error occurs, as the @-operator hides it. If the external library is large, it becomes a headache to remove all the @ from the code. One nice option I found is the Scream Pecl extension. The extension allows you to easily disable the @-operator in your code without making any actual changes to the code.
As a pre-complied binary is not available, you need to make it yourself. The following shows commands to compile the extension on Ubuntu.
First you need to install the Pear distribution environment.
sudo apt-get install php-pear
Next you will need to install the php5-dev package to get the required PHP5 source files to compile additional modules.
sudo apt-get install php5-dev
Finally we are ready to actually create and install the extension.
sudo pecl install scream-0.1.0
Once the extension is created and installed, we need to add one to the php.ini file.
sudo gedit /etc/php5/apache2/php.ini
In the ‘extensions’ section add the following line:
After the php.ini has been updated, you need to restart Apache, so that the new extension is loaded.
sudo /etc/init.d/apache2 restart
If hopefully all went well, the Scream extension should now be loaded, which you can confirm using phpinfo().
Now you can disable the @-operator in your code using the following:
ini_set('display_errors', 1); error_reporting(E_ALL | E_STRICT); // Disable the @-operator ini_set('scream.enabled', true); $var = @ $_GET['data'];
Or you can directly enable the extension in your php.ini.
Now even though the silence operator is present the above code generates an error if the ‘data’ parameter is not set. Atlast no need to hunt down for @’s while debugging.
This site is a digital habitat of Sameer, a freelance web developer working in PHP & MySQL.More