Functions deprecated? Suppress warning messages in PHP

Ruins for developers

Things are built on the foundations of their predecessors. St. Peter’s in the Vatican was built on the ruins of the Circus of Nero. It is estimated that over 600 ships from the days of Yerba Buena are buried under the city of San Francisco. Isaac Newton was quoted as saying, “If I have seen further, it is by standing on the shoulders of Giants.”

This happens to both natural and computer languages, as well. As a constantly developing language, newer PHP versions include functions that become deprecated. These functions cease to exist or change the expected result of the function as further versions of PHP are released. These changes can result in warnings and error messages when you update your version of PHP and run existing code. Using these deprecated functions will result in the creation of warnings or errors, which can be problematic, or at least annoying. The code may still run, but it might result in unusual behaviors.

Modifying the error reporting helps treat symptoms of deprecated functions, but it’s not the same as updating the code. Keeping your code updated is the preferred solution.

Several common issues can arise when using deprecated functions:

1. Functions just flat-out stop working. Very few functions get completely removed from PHP, but sometimes it happens. Applications or scripts might rely on functions that are simply no longer supported. PHP.net has a page for each function describing its use, which includes information about when or if a function was deprecated or removed. In these cases, it suggests which functions could be used instead, or which functions were meant to replace the deprecated version.

2. Warning messages display about deprecation. These warning messages don’t normally interfere with site functionality. However, in some cases, they might disrupt the process of the server sending headers. This can cause login issues (cookies/sessions don’t get set properly) or forwarding issues (301/302/303 redirects use headers to instruct the browser).

Don’t mention it

If you receive errors regarding function deprecation, the following two methods can tell PHP to simply stop mentioning deprecated functions or coding:

You can add the following line to your php5.ini file:

 error_reporting = E_ALL & ~E_DEPRECATED & ~E_NOTICE

Or you may add the following line to a PHP file itself, inside existing or new <?php ?> tags:

error_reporting(E_ALL & ~E_DEPRECATED & ~E_NOTICE);

Fatal and parser error messages

This leaves on important PHP error reporting such as Fatal Errors or Parser Errors. Fatal Errors are things such as “I tried to perform that command but I’m not sure what that command is” or “I ran out of time to work on running these instructions.” Parser Errors are things such as “I was not expecting this comma in the middle of the line” or “Why is there a variable here, I was already done with this line.” These errors completely prevent a file from working correctly, so it’s best you leave them on.

If somehow all else fails and you need to turn off all error messages, you can add the following line to your php5.ini file:

display_errors = Off

or you may add the following line to a PHP file itself, inside existing or new <?php ?> tags:

error_reporting(0);

Do note that this means you’ll get no indication of why a file failed to perform its expected function, so typically this is not the solution you’re looking for.

Single-serving suppression

One more tip regarding error reporting in PHP: If you are interested in suppressing a specific error message, you can do so for even a single time that a function is used. For instance, running this in PHP 5.3 or later:

split('l','hello');

gives you the error message

Deprecated: Function split() is deprecated in /test.php on line 1

This error message conveniently tells you exactly what line uses the offending function.

Updating the line to say:

@split('l','hello');

suppresses the error message. The @ symbol instructs the function to try, and not report if it fails.

As noted above, the tips here are useful mainly as a stopgap measure when you have running code that depends on older functions that have been deprecated, and can keep you going in a pinch. The preferred method, however, is to use newer functions or lightly refactor the code so it doesn’t depend on deprecated functions.


Also published on Medium.

Image by: fchmksfkcb via Compfight cc

Christopher Carfi
A veteran of both startups and the enterprise, Chris has a deep track record in developing customer community and evangelist programs for brands such as Adobe, H&R Block and Aruba Networks while holding executive positions at Ant’s Eye View and Edelman Digital, and he was co-founder and CEO at Cerado. He currently lives in the Bay Area with his family.