How to display All Errors and warnings in PHP?

Sometimes we wonder, why php is not showing the errors, warning or any notices? Well the answer lies in the PHP configuration or ini.

To simply turn on all the Errors and warnings, put the code below in your script:

error_reporting(E_ALL);
ini_set('display_errors', '1');

or If you are running an old version of PHP then:

error_reporting(-1);
ini_set('display_errors', '1');

Passing the -1 value will show every possible error, even when new levels and constants are added in future PHP versions. From PHP >= 5.4, the E_ALL constant also behaves in the same way.

So, lets talk about these configurations:

error_reporting

error_reporting is a PHP configuration flag that is used to set the error reporting level. The parameter is either an integer representing a bit field, or named constants. See, the full list of available constants. Setting the flag to E_ALL will log all the errors and warnings.

Same as error_reporting(E_ALL) we can also use ini_set('error_reporting', E_ALL);

If we want all errors except Notices, then we should use:

error_reporting(E_ALL & ~E_NOTICE);

display_errors

display_errors is also a configuration directive in php, that determines whether errors should be printed to the screen as part of the output or if they should be hidden from the user. Although, the default value for display_errors is 1 but you need to ensure that it is not turned off, like by your Infrastructure guy or if you are on shared system.

If you have edit access to the php.ini then you can set this value directly in your ini else via code using ini_set. Remember, this will be permanent setting for your PHP environment. So avoid using these settings on Production, It is for development environment otherwise you will be ended up showing all your errors, warning and notices to your valued customers.

From PHP >= 5.2.4 onwards we can also set display_errors value to “stderr” to send the errors to stderr instead of stdout. Prior to that this was of type boolean.

Setting these configurations in dev environments help immensely in development and debugging. In further posts, we will see how to do debugging in PHP. So stay tuned !!

Leave a Reply

Your email address will not be published. Required fields are marked *