fbpx

What is Display PHP Errors? Basics and Advanced

As a developer, you may face many errors every day. But as a user, you may also face errors on your application. PHP applications generate several levels of errors and warnings during execution. 

So the question is why display PHP errors? And the answer is displaying PHP errors is key for developers seeking to troubleshoot a misbehaving application. It is often difficult for developers to display PHP errors in applications. As a result, their applications simply stop working and they don’t know how to debug and fix the issues.

There are many cases of PHP errors, including some errors that just stop the application from working and some that just show notice and don’t affect the application. This article explains how to debug PHP errors and how many different types of PHP errors exist, so you will know about PHP errors and how to detect them.   

Types of PHP Errors:

Following are the four different types of PHP errors.

Notice Error:

Notice Errors are small errors that do not stop the execution of your application. Notice errors are just like warnings that show something but do not affect the working flow. In PHP scripts, a notice error occurs if an undefined variable is attempted to be accessed. There is just an alert that will let you know that there is a problem.

Display PHP errors

Warning Error: 

Just like Notice Errors Warning Errors also do not stop the execution of your application. Basically, it occurs when a PHP function calls/includes a file that does not exist. It does not stop the program from running. The main cause of generating a warning error is supplying an incorrect number of parameters to a function or omitting a file.

Warning Error PHP Errors

Fatal Error

Unlike warnings and notices, Fatal errors do not allow the application to execute normally. This is the type of error where the PHP compiler understands the PHP code but it recognizes an undeclared function. This means that the function is being called without its definition. This error is basically due to undefined functions and classes.

Fatal Error

Parse Error:

A parse error is also called a syntax error. If the PHP code contains a syntax error, the PHP parser cannot understand the code and stops working.

If you are facing any issues with debugging and resolving PHP errors and have trouble while working with error reporting. You want to display errors PHP. This tutorial will show you how to enable PHP errors and warnings.

Quickly Display PHP Errors:

You can display all PHP errors and warnings by adding these lines to your PHP code file:

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

Let’s describe the above code line by line.

ini_set()

Ini_set attempts to override the configuration found in the PHP .ini file.

display_errors

The display_errors directive controls whether errors are displayed to the user or not. Usually, this should be disabled after development.

display_startup_errors

The display_startup_errors directive is used to find errors encountered during PHP startup. The display_errors directive does not handle such errors, so this is a separate directive.

Display_errors and display_startup_errors do not show parse errors such as missing semicolons or curly braces. To fix this, modify the PHP.ini configuration.

error_reporting()

Error reporting is handled by the error_reporting function, which is a native PHP function. PHP scripts can use this function to report all types of errors. In this case, the named constant E_ALL is used as an argument.

.htaccess Configuration to Show PHP Errors:

Note: This method to display php errors does not work with OpenLiteSpeed.

Files in directories are usually accessible to developers. The .htaccess file, located in the root of the project or the public directory, can also be used to activate or disable the directive for showing PHP errors. The .htaccess file is one of the easy ways to use for PHP error handling.

Add these lines or edit these in .htaccess to display PHP errors.

php_flag display_startup_errors on
php_flag display_errors on

Display_startup_errors and display_errors directives will be added to the .htaccess file. In this way, development and production can have different .htaccess files by showing or disabling error messages, with production suppressing the display of errors.

Depending on which files you have access to and how you handle deployments and server configurations, you may need to adjust display_errors in .htaccess or PHP.ini. Most hosting companies won’t allow you to change the PHP.ini file to display php errors.

In CloudPages, you can use this method to display errors from your website dashboard. To do this just go to the website dashboard and then the file manager of CloudPages. Click on List Website-> Website -> File Manager.

Now go to the public_html and then edit your .htaccess file from there by clicking on the file and add these lines there after adding these lines click on the Save Changes button..

Configure PHP.ini to Display All PHP Errors:

When adding PHP errors to the browser, they may not appear in the browser during testing. If this is the case, then the PHP ini configuration has some additional directives to handle it.

Add the line or edit this line in your php.ini file. The location of php.ini is different in different scenarios. Like in case of LiteSpeed Webservers the location of PHP.ini depends upon the PHP version you are using the location is:

/usr/local/lsws/lsphp74/etc/php.ini
display_errors = on

The display_errors directive must be enabled in the PHP ini file. This will display php errors including syntax or parse errors that cannot be displayed by simply calling the ini_set function in the PHP code. If the web application is to be used in production, the loading configuration file directive in the ini configuration must be set to off in the PHP ini file. This directive can be found in the displayed output of the phpinfo() function.

To edit and open PHP.ini file from the terminal just run the command:

nano /usr/local/lsws/lsphp74/etc/php.ini 

Edit this file by adding debug and save it using CTRL+X.

Enable Detailed Warnings and Notices:

Some warnings that do not seem to affect the application at first can cause fatal errors in certain circumstances. This means that the application may not function normally under certain conditions.

There is no harm in hiding the warnings and just displaying what the warning message is if there are a lot of errors caused by these warnings.

error_reporting(E_WARNING);

A developer can easily show warnings and hide errors by adding a single line of code. The parameter for the php error reporting function will be “E_WARNING | E_NOTICE” to display warnings and notifications respectively. As bitwise operators, the error reporting function can accept E_ERROR, E_WARNING, E_PARSE, and E_NOTICE parameters.

In-depth with the error_reporting() Function:

Developers can select which errors and how many should be displayed using the PHP error_reporting() function. During runtime, this function sets the error reporting directive in the PHP ini configuration.

error_reporting(0);

The error_reporting() function should be called with the value zero to delete all errors, warnings, parse messages, and notices. It would be inconvenient to include this line of code in every PHP file. The .htaccess file or the PHP ini file should be modified to disable report messages.

error_reporting(-1);

The error_reporting() function also accepts an integer value as input. Using this method, we can display errors in PHP. PHP has a variety of error types. PHP errors are represented by a -1 level. Even with new levels and constants, passing the value -1 will still work in future PHP versions.

error_reporting(E_NOTICE);

Even variables not declared in PHP can be used. In loops and conditions, undeclared variables can cause issues in the application. It may also happen if the variable stated is spelled differently than the variable used in conditions or loops. The web application will display these undeclared variables if E_NOTICE is supplied in the error_reporting() function.

error_reporting(E_ALL & ~E_NOTICE);

The error_reporting() function allows you to filter which errors are displayed. As the “*” character stands for “not” or “no,” the parameter ~E_NOTICE indicates that notices should not be displayed.

In the code, look for the letters “&” and “|.” The “&” symbol stands for “true for all,” while the “|” stands for “true for either.” In PHP, these two characters mean the same thing: OR and AND.

Use Web Server Log Files for PHP Errors:

Instead of using .htaccess and php.in files, you can check the webserver logs files for better understanding and debugging. In LiteSpeed webserver location is different but the main looks like this.

ErrorLog "/usr/local/lsws/logs/error.log"

For all web servers and especially the Litespeed webserver the error_logs are there in the logs directory. For this web server log files are writeable and already there you you do the installation. to display php errors this one also helps you to dig in deep.

After reading about these PHP errors maybe you also want to learn about WP 301 Redirects – Instantly Fix Your SEO Errors.

Become CloudPages

Community Member to Get Latest Updates.

Pin It on Pinterest

Share This
Scroll to Top
Scroll to Top