Home / Prevent E_DEPRECATED error messages in PHP

Prevent E_DEPRECATED error messages in PHP

PHP 5.3 introduced a new error reporting level E_DEPRECATED which is triggered when deprecated functions and methods are used, such as the old style ereg() regular expression functions. This post shows how to suppress E_DEPRECATED error messages.

In PHP.ini

To show all errors other than E_DEPRECATED in the php.ini file, adjust the error_reporting setting as shown below. Note this should only be done for installs of PHP 5.3+.

error_reporting = E_ALL & ~E_DEPRECATED

To suppress notices as well:

error_reporting = E_ALL & ~E_NOTICE & ~E_DEPRECATED

In code with error_reporting

You can either straight out set the error reporting level like so:

error_reporting(E_ALL &~ E_DEPRECATED);
error_reporting(E_ALL &~ E_NOTICE &~ E_DEPRECATED);

or remove E_DEPRECATED from the current error_reporting level:

error_reporting(error_reporting() & ~E_DEPRECATED);

Making it safe for earlier versions of PHP

The only catch with the above error_reporting examples is that if you’re running the same code on e.g. PHP 5.2 as well as PHP 5.3 then you’ll get a notice in PHP 5.2 (and earlier) like "Notice: Use of undefined constant E_DEPRECATED".

To avoid triggering this notice check if E_DEPRECATED is defined:

if(defined('E_DEPRECATED')) {
    error_reporting(E_ALL &~ E_DEPRECATED);
}
if(defined('E_DEPRECATED')) {
    error_reporting(error_reporting() & ~E_DEPRECATED);
}

Development vs production

While you certainly won’t want to trigger E_DEPRECATED messages in production, you may well want to show them in development to make it easy to locate and update code with deprecated functions. (Note you can suppress the display of error messages regardless of the error_reporting level with display_errors).

In my case, I’ve been using SilverStripe 2.4 which occasionally makes use of the ereg() functions and I prefer not to have the messages displayed even in development. SS 3 will come out later this year and I’m sure they’ll have replaced the ereg functions with preg equivilents. Maybe then I’ll switch back E_DEPRECATED in development.