Handling All PHP Errors

Benson Arafat
4 min readJul 27, 2022

--

Sometimes your PHP application might produce many different types of earnings and errors or sometimes you might see a blank page which might not understand if you are getting an error or not.

In this article, we will discuss all PHP errors, warnings and how you can turn on/off these errors. So if you are having problems with your PHP application and need to display all the errors and warnings, you are in the right place, we will also discuss how to event collect them when your site is now online.

Display all PHP errors

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

Now, let’s explain what these lines mean in plain text

ini_set function

The ini_set function allows you to override the configuration found in your PHP.ini file. The display_errors option will determine if the errors will be displayed or hidden. It’s important that this error mode be turned off during production.

So how can you use this? If you want to display errors you simply pass a value 1 to hide errors and pass a value 0

ini_set('display_errors', 1); //display errors 
ini_set('display_errors', 0); //hide errors

Note: Using this won't be able to display parse errors such as missing semicolons or missing curly braces. In this case, you have modifiy your PHP ini configuration

Setting up your PHP.ini to display errors

If you can’t see errors after using the ini_set to display errors, don’t worry you can still do that by going to your PHP.ini file

How to locate your PHP.ini file

If you are using xampp, you can find your PHP.ini file with these steps.

  • Open the directory where you install your xampp.
  • Look for a folder called PHP
  • In the PHP folder, you can scroll down or use the search option to locate the php.ini file.

Turn on display errors in the PHP.ini file

If you have successfully located the PHP.ini file bravo, all we have to do is to open the file with a text editor and search for display_errors then we change its value to on.

display_errors = on

Note: After we have made a change on the php.ini file and saved the file, we must restart our server.

Display PHP Errors via .htaccess Configuration

You can also enable or disable error mode using the .htaccess file located in the root or public directory of the project

php_flag display_startup_errors on 
php_flag display_errors on

This is the same as you add to the PHP code to show PHP errors. Depending on which files you have access to and how you do deployments and server configurations, you may want to configure display_erros in .htaccess or your PHP.ini file. Many hosting providers will not allow you to modify your PHP.ini file to enable display_errors.

In the .htaccess file, a custom error log can also be enabled as long as the log folder or the log file is writable by the web server. The log file can be a relative path to where the .htaccess is located, or it can be an absolute path such as

/var/www/html/website/public/logs

php_value error_log logs/all_errors.log

PHP Warnings and Notices

Most times, the warnings don’t affect our application but will cause some fatal errors in certain conditions. So these errors must be fixed because this means that the application won’t run normally under certain scenarios. In case these warnings cause a lot of errors, then it would be more practical to hide the errors and just show the warning messages.

error_reporting(E_WARNING);

Hiding and showing a warning is just as simple as adding a single line of code. To show warnings and notices, the parameter for the error reporting function will be E_WARNING | E_NOTICE.

The error_reporting function can also accept E_ERROR and E_PARSE parameters as bitwise operators. To report all errors except for notices, then the parameter is E_ALL & ~E_NOTICE where E_ALL stands for all possible parameters of the error reporting function.

error_reporting(0);

To remove all errors, warnings, and parse message notices, the parameter that should be passed to the error_reporting function is zero (0). It would be not practical to have this line of code in each of the PHP files. it would be better to turn off report messages in the PHP ini file or in the .htaccess.

error_reporting(E_NOTICE);

PHP allows variables to be used even when not declared. This is not standard practice because undeclared variables will cause issues for the application once it is used in loops and conditions.

Sometimes this also happens because the declared variable has a different spelling than the variable being used for conditions or loops. When E_NOTICE is passed in the error_reporting function, then these undeclared variables will be displayed in the web application.

error_reporting(E_ALL & ~E_NOTICE);

the error reporting function allows you to filter which errors can be shown. The “~” character means “note” so the parameter ~E_NOTICE means not to show notices. Notice the “&” and “|” characters in between the possible parameters. The “&” character is for “true for all”, while the “|” character represents either one as long as it is true. These two characters have the same meaning in PHP conditions OR and AND.

Hope this helps you understand how to handle PHP warnings and errors, if you find anything which is not clear you can leave a comment below.

Thanks.

--

--

Responses (1)