PHP – How to disable error log, display errors and error reporting programmatically

PHP - How to disable error log, display errors and error reporting programmatically

Every PHP developer, system administrator and webmaster knows that the best way to debug PHP scripts is to show and/or log its errors by settings the appropriate values to the error_reportingdisplay_errors and log_errors directives in the PHP.INI file.

Here’s a typical scenario for a production web server:

  • The first command instructs PHP to take into consideration all kind of warnings minus the DEPRECATED and STRICT ones: the DEPRECATED warnings are related to code that might cease to work in future versions, while the STRICT ones are about code that could cause interoperability or forward compatibility issues.
  • The second command prevents PHP from showing the error to the end-user, which is something we almost always want to avoid in production scenarios.
  • The third command tells PHP to log the errors to secure locations such as STDERR or a local file which has to be specified by an additional error_log directive.

What if we want to override these settings programmatically? For example, change the default behaviour within a single page or a group of pages?

Luckily enough, we can do this by using the ini_set() function, which allows to change most configuration directives programmatically.

Here’s the required code:

That’s about it: it’s worth noting that this will work on either PHP 5.x and PHP 7.x.

 

RELATED POSTS

About Ryan

IT Project Manager, Web Interface Architect and Lead Developer for many high-traffic web sites & services hosted in Italy and Europe. Since 2010 it's also a lead designer for many App and games for Android, iOS and Windows Phone mobile devices for a number of italian companies.

View all posts by Ryan