Banner

Monday, 2 February 2015

SUPPRESS WARNING MESSAGES IN PHP

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.

No comments:

Post a Comment