PHP provides numerous error configuration options in the php.ini file. Since many people do not have the option of editing their ini file I would like to concentrate this series of posts on the ones which can be set within scripts (at run-time), more specifically ones you should consider using when creating a strategy for handling errors. If you would like a complete list of the configuration options within PHP, the manual provides a very nice chart.
The configuration option I would like to address in this article is, of course, error_reporting.
Let’s start digging.
error_reporting
The error_reporting directive (within the PHP ini file) allows you to set the level of errors reported by PHP. By default this is set to ‘E_ALL & ~E_NOTICE’. This ‘tells’ PHP to report all errors, except Notices.
PHP provides a built-in function for getting and setting the error_reporting level: error_reporting(). The function signature looks like this:
int error_reporting ([ int $level ] )
The function accepts one optional argument, a new error reporting level, and returns the old error reporting level. Although the signiture shows that the function accepts an integer, the new level sent can be represented by a predefined constant. In fact it is recommended that you use the predefined constants to ensure future compatibility.
The available constants can be used in combination to build up a bitmask; the bitmask tells PHP which errors to report and which to suppress. Since these constants are actually bitmasks, you will need to use bitwise operators when combining them.
An example of the error reporting setting I use in development is:
$old_setting = error_reporting(E_ALL | E_STRICT);
From the predefined constants chart the setting E_ALL equates to the integer 30719 and means:
All errors and warnings, as supported, except of level E_STRICT in PHP < 6.
From the predefined constants chart the setting E_STRICT equates to the integer 2048 and means:
Enable to have PHP suggest changes to your code which will ensure the best interoperability and forward compatibility of your code.
So basically, this tell PHP to report all errors, notices, and messages generated at the E_STRICT level.
Back to the code…
The variable $old_setting will now be set to: 6135, the default setting for error reporting (using the predefined constants again that would be: E_ALL & ~E_NOTICE).
A small code example:
<?php
//get the current error reporting setting
$current_setting = error_reporting();
//check if error reporting is set to E_ALL | E_STRICT
if (8191 != $current_setting) {
//not set to E_ALL | E_STRICT make it so
error_reporting(E_ALL | E_STRICT);
}
//display new error reporting setting
echo error_reporting(); //displays 8191
?>
A more useful example would be to change the error_reporting setting based on the environment the script is being run in.
Let’s say you have a configuration file which defines the constant ‘ENV’, this can be set to ‘production’ (live site) or ‘development’. If the script is run in production then we want to use ‘E_ALL & ~E_NOTICE’ (default), but when running in development we want to use E_ALL | E_STRICT.
config.php:
<?php
//define enviroment
define('ENV', 'development');
Script File:
<?php
//load configuration file
require_once('config.php');
//check ENV constant to see if running in development
if ('development' == ENV) {
//running in development so change default setting
error_reporting(E_ALL | E_STRICT);
//now all errors, notices, and suggestions will be reported
}
That’s it! I’m sure you can see how useful this function can be.
Side Note: If you are unsure what a bitmask is or are unfamiliar with bitwise operators, you may want to checkout this post from joestump.net.



PDF













