≡ Menu

How to control display of error messages in PHP?

The question – I want to disable all error reporting in my PHP script. I don’t want PHP to report any errors to the end user on his browser. Is there a PHP command or directive that can be used to do this?

PHP has a built in function called error_reporting which can be used to enable / disable error reporting.

In the beginning of your PHP script, add the following to disable all error reporting:

error_reporting(0);

To enable all error reporting, add the following to the beginning of your PHP script:

error_reporting(E_ALL);

The constant E_ALL instructs PHP to report all PHP errors including warnings to the standard output, which is the browser in case PHP is run through a web server like Apache.

In most cases, you do not want to display warnings. In this case, you can use the following syntax which will cause PHP to report only errors.

error_reporting(E_ERROR | E_PARSE);

This will report all run time errors and parse errors to the user’s browser.

On my server, I enable error reporting to E_ALL when I am debugging my scripts. Once I know that everything is working fine, I disable all error reporting by the error_reporting(0) statement.

Comments on this entry are closed.