www.php.net › manual › function.error-reporting.php. How do I get PHP errors to display ; 1. //PHP functions ; 2. ini_set('display_errors', 1); ; 3. ini_set('display_startup_errors', 1); ; 4. error_reporting(E_ALL);. If you want to quickly show all the PHP errors and warnings, then you should add the following.
Display errors php ini_set - opinion
PHP show errors in the browser
By default, PHP will try to keep all the warnings and errors from showing on your browser.
This is done so that your users won’t see the errors when using your application.
But to fix an issue found in your code, you may need to display the PHP errors and warnings so that you know what is wrong with your application.
This tutorial will help you to display PHP errors on the browser.
Enable error reporting on a PHP file
To display errors and warnings in your browser, you need to set the following directives at the top of your PHP code file:
The function is used to override the configuration in your file.
In the above example, the and configuration is activated by setting their values to or .
Usually, these configurations are set to to hide the errors and warnings in your PHP code execution.
Finally, the function determines the error level that will be generated.
There are many different levels to PHP error reporting as follows:
- - show all errors and warnings
- - show only fatal run-time errors
- - almost fatal run-time errors
- - run-time warnings (non-fatal errors)
- - compile-time parse errors
- - show run-time notices
- - enable PHP to suggest best practice in your code
- - fatal errors that occur during PHP’s initial startup
- - warnings that occur during initial startup
- - fatal compile-time errors
- - compile-time warnings (non-fatal errors)
- - user-generated error message
- - user-generated warning message
- - user-generated notice message
- - warn about features that will be dropped in future PHP versions
- - user-generated deprecation warnings
But usually, only the option is used for debugging your code.
With the three directives set, you should see errors and warnings in your code.
Suppose you have a PHP script as follows:
When calling the file from the browser, you will see warning messages because the is undefined as shown below:
But keep in mind that setting the directives in the PHP file won’t work when you have a parse error.
Enable error reporting from PHP ini file
A parse error happens when your PHP script is wrong at the syntax level, such as missing semicolons in your code.
Suppose you miss a semicolon as shown below:
Then the page will fail to load on the browser and it will show an HTTP 500 error code:
To make PHP show display parse errors, you need to change the configuration settings in the file.
If you don’t know where the configuration file is located, execute the following code from the terminal:
You should see the location of your file as shown below:
Next, open the file and set the , , and configs as shown below:
The configs above are exactly what you set using the and functions.
By setting the configs from the file, the parse errors won’t cause PHP to return the error 500 code.
Restart your PHP server, then run the same file again. This time, you should see a parse error message as follows:
That’s how you display all PHP errors and warnings using the PHP ini file.
Alternatively, you can also display PHP errors using the configuration.
Display PHP error from the htaccess file
Sometimes, you may not be able to edit the file because of restrictions from your hosting provider.
In that case, you can try to add error logging configurations from the file.
The file is a configuration file that you can use to instruct your web server.
This file is usually located in the root directory of your website.
Put the following configs in your file:
Save the configurations, and your web server will follow the instructions the next time you run PHP code.
If that doesn’t work, try restarting your web server.
Now you’ve learned how to get PHP to display errors and warnings. Good work! 👍
5. Use error_log() Function to Log PHP Error
Error messages must not be displayed to end-users during production, but they must be logged for tracing purposes. The best approach to keep track of these error messages in a live web application is to keep track of them in log files.
The error_log() function, which accepts four parameters, provides a simple way to use log files. The first parameter, which contains information about the log errors or what should be logged, is the only one that must be provided. This function's type, destination, and header parameters are all optional.
If the type option is not specified, it defaults to 0, which means that this log information will be appended to the web server's log file.
The error logs supplied in the third parameter will be emailed using the type 1 parameter. To use this feature, the PHP ini must have a valid SMTP configuration to send emails.
Host, encryption type, username, password, and port are among the SMTP ini directives. This kind of error reporting is recommended for logging or notifying errors that must be corrected as soon as possible.
Type 3 must be used to log messages in a different file established by the web server's configuration. The third parameter specifies the log file's location, which must be writable by the webserver. The log file's location might be either a relative or absolute library to where this code is invoked.
You can also refer to the error_log() function documentation to know more.
6. Use Web Server Configuration to Log PHP Errors
The ideal technique to record errors is to define them in the web server configuration file, rather than modifying parameters in the .htaccess or adding lines in the PHP code to show errors.
These files must be added to the virtual host of a specific HTML page or application in Apache, which is commonly found in the sites-available folder in Ubuntu or the httpd-vhosts file in Windows.
The directive is just called error_log() in Nginx, as it is in Apache. The log files for both Apache and Nginx web servers must be writable by the webserver. Fortunately, the folders for the log files of these two web servers are already writable after installation.
7. Collect PHP Errors using Atatus
Atatus is an Application Performance Management (APM) solution that collects all requests to your PHP applications without requiring you to change your source code. However, the tool does more than just keep track of your application's performance. Atatus' ability to automatically gather all unhandled errors in your PHP application is one of its best features.
To fix PHP exceptions, gather all necessary information such as class, message, URL, request agent, version, and so on. Every PHP error is logged and captured with a full stack trace, with the precise line of source code marked, to make bug fixes easy.
Read the guide to know more about PHP Performance Monitoring.
All errors are automatically logged and structured in Atatus so that they can be easily viewed. Not only will Atatus show you what errors have happened, but it will also examine where and why they occurred. The logs also display the time and number of occurrences, making it much easier to focus on which issue to address.
Start your 14-day free trial of Atatus, right now!!!
When there is an issue with the PHP code, a PHP error occurs. Even something as simple as incorrect syntax or forgetting a semicolon can result in an error, prompting a notification. Alternatively, the cause could be more complicated, such as invoking an incorrect variable, which can result in a fatal error and cause your system to crash.
This tutorial has shown you several ways to enable and display all PHP errors and warnings. You can boost your ability to debug PHP issues by receiving error notifications fast and precisely.
Let us know what you think about displaying PHP errors in the below comment section.