Login |  Register 
I thought I knew all about PHP until I found this website!
Jen
 

Supressing Errors

Sometimes you want errors thrown by your code to be suppressed, perhaps because you are handling this in another way.

Error Control Operator

The error control operator is @. This is appended to the front of expressions so variables, functions and include calls.

However you can not use the error control operator to where something is declared so adding it in front of a function or class definition.

Futhermore this operator only supresses the messages of a expression, it doesn't supress exceptions, these will still be thrown as normal. An example of this in action is shown below.

$results = @mysql_select("SELECT * FROM Users");

So the above code will ensure that no warnings or notices are thrown by the above function.

However you need to be careful that the error control operator isn't abused, it should be used to stop errors being thrown where you know it is safe to ignore them not as a general way of ignoring potential problems, at the expense of writing defensive code.

$name = null;
if (isset($myname))
{
 $name = $myname;
}

The above code checks to see if a variable is set before assigning it to the $name because the programmer is unsure if it will be set, so wants to stop a error message appearing. However this error can be ignored by using the error control operator like so:

$name = @$myname;

Unfortunatley using the error supression method is twice as slow, as the isset. Therefore you shouldn't use the error control operator, as a short cut around defensive programming logic tests

Error reporting

You can also hide erors generally by using the error_reporting function for a script.

This function will restrict what type of errors are shown

error_reporting(0);

The above code turns off all error reporting for a script, however you can also allow certain types of errors by using bitwise operators along with the error types.

Value Constant Description
1 E_ERROR Using this option will supress any fatal errors, these are errors where the script can't usually continue.
2 E_WARNING This option allows you to supress warnings, which are not fatal but are detail problems with the code, which could in certain circumstances be serious.
4 E_PARSE This option allows you to supress parsing errors, which are errors that occur when PHP tries to parse the script during compile time, but fails for some reason.
8 E_NOTICE Indicate that the script encountered something that could be an error, but could happen normally when running a script.

Therefore using the above values different error messages can be suppresed as shown below:

error_reporting(E_ERROR | E_WARNING);

This will report fatal errors and warnings

error_reporting(E_NOTICE);

Reports everything except notices. Therefore you can add as many exclusions to a script in the above fashion, as you like using the | operator.

Remember that while supressing errors may in some cases be necessary, it is always better to first try and prevent the error, rather than just suppressing it. This should make your script more efficent.


No comments have been provided.
security image
Written by Dominic Skinner
Last Updated: 2011-10-25 16:00:38