Problem:You run a php script and it displays nothing – just a blank white screen. It happens due to any error occurs in that script. You know changing few things in the php.ini file will display the errors in the browser which will help you to identify the problem quickly. Show
Solution:By default, the error reporting is disabled. Hiding error messages, browsers prevent showing any sensitive server information. Though, in the development phase, developers find these error messages helpful to accelerate the development. To enable error messages, it requires few changes in the php.ini file. Follow the steps below- Step 1: Locate the php.ini file
Step 2: Enable error displaying optionIn the php.ini file, search with “Error handling and logging” string. You’ll see something like the following image- To enable errors to display in the browser, remove semicolons (;) in front of the following two lines- ; display_errors = On; display_startup_errors = On The first line will enable displaying errors in the browser and the second line will enable displaying PHP’s startup sequence errors . The previous line can’t handle the startup sequence errors. So, it will look like the following picture- Step 3: Decide which errors to displayTo display all possible errors, enable one of the following three error reporting lines in the php.ini file depending on your PHP version.
After enabling the above line in the php.ini file, it will look like the following picture – Step 4: Enable error log fileTo log any errors in the web server’s error log file, uncomment the following line in the php.ini file- It will look like the following picture- In the production level, you must turn off the displaying error messages in the browser. But, as the error log file will remain be enabled, any error if ever occurs will be saved in the server’s log file. You can check that file to see error information. How to find the error log fileYou can find the location of your error log file in the php.ini file. Look for a line starting with “error_log = ”. For my local machine the line look like- error_log = “F:\xampp\php\logs\php_error_log” There are other methods to
accomplish the same task. Check the following- Introduction PHP is a server-side scripting language used in web development. As a scripting language, PHP is used to write code (or scripts) to perform tasks. If a script encounters an error, PHP can generate an error to a log file. In this tutorial, learn how to enable PHP Error Reporting to display all warnings. We also dive into creating an error log file in PHP. What is a PHP Error?A PHP error occurs when there is an issue within the PHP code. Even something simple can cause an error, such as using incorrect syntax or forgetting a semicolon, which prompts a notice. Or, the cause may be more complex, such as calling an improper variable, which can lead to a fatal error that crashes your system. If you do not see errors, you may need to enable error reporting. To enable error reporting in PHP, edit your PHP code file, and add the following lines:
You can also use the ini_set command to enable error reporting:
Edit php.ini to Enable PHP Error ReportingIf you have set your PHP code to display errors and they are still not visible, you may need to make a change in your php.ini file. On Linux distributions, the file is usually located in /etc/php.ini folder. Open php.ini in a text editor. Then, edit the This is an example of the correction in a text editor: Edit .htaccess File to turn on Error ReportingThe .htaccess file, which acts as a master configuration file, is usually found in the root or public directory. The dot at the beginning means it’s hidden. If you’re using a file manager, you’ll need to edit the settings to see the hidden files. Open the .htaccess file for editing, and add the following:
If these values are already listed, make sure they’re set to on. Save the file and exit. Other Useful CommandsTo display only the fatal warning and parse errors, use the following:
You can add any other error types you need. Just separate them with the pipe | symbol. This list contains all the predefined constants for PHP error types. One useful feature is the “not” symbol. To exclude a particular error type from reporting:
In this example, the output displays all errors except for notice errors. How to Turn Off PHP Error ReportingTo turn off or disable error reporting in PHP, set the value to zero. For example, use the code snippet:
How to Create an Error Log File in PHPError logs are valuable resources when dealing with PHP issues. To display PHP error logs, edit the .htaccess file by adding the following:
If you don’t have access to the .htaccess file, you can edit the httpd.conf or apache2.conf file directly. This log is typically stored in the /var/log/httpd/ or /var/log/apache2/ directory. To enable error logging, edit your version of the file and add the following:
You may substitute httpd for apache2 if needed. Likewise, if you’re using nginx, you can use that directory for the error log. How to Display PHP Errors on a WebpageError logs are valuable resources when dealing with PHP issues. To display PHP error logs, edit the .htaccess file by adding the following:
If you don’t have access to the file, you can edit the httpd.conf or apache2.conf file directly. This log is typically stored in the /var/log/httpd/ or /var/log/apache2/ directory. To enable error logging, edit your version of the file and add the following:
You may substitute httpd for apache2 if needed. Likewise, if you’re using nginx, you can use that directory for the error log. Conclusion This tutorial has provided you with multiple alternatives to enable and show all PHP errors and warnings. By receiving error notifications quickly and accurately, you can improve the ability to troubleshoot PHP issues. If you are implementing new features, installing a new PHP-based app, or trying to locate a bug on your website, it is important to know which PHP version your web server is running before taking any steps. How do I track PHP errors?Quickly Show All PHP Errors
The quickest way to display all php errors and warnings is to add these lines to your PHP code file: ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);
How do I view php error logs?Look for the entry Configuration File (php.
Find the Error handling and logging section of the php. ini file. Make sure that both display_errors = On, display_startup_errors = On and log_errors = On are present and uncommented. Check the value of error_log - this tells you the location of the file errors are logged to.
How do I fix PHP errors?Editing the php.. Log into your cPanel.. Go to the File Manager. ... . Find the “Error handling and logging” section in the php.ini. ... . Next you can set the display_errors variable to On or Off to either show the errors on your website or not.. How do I turn off error reporting in xampp?In order to modify the way Xampp shows errors you have to go control panel and open php. ini. Inside this file you can find two points to modify the way it shows errors: “display_errors = On”.
|