PHP
Published in PHP
avatar
3 minutes read

Getting PHP Errors to Display

To debug and troubleshoot PHP code effectively, it's essential to display PHP errors. By default, PHP may suppress error messages from being shown on the web page for security reasons.

Enabling Error Reporting

Error Reporting Configuration

To enable error reporting in PHP, you need to adjust the error_reporting and display_errors directives in your PHP configuration or within your PHP script.

Using php.ini Configuration

  1. Locate your php.ini file. You can find its location by creating a PHP file with the following content and running it on your server:
<?php
phpinfo();
?>
  1. Search for the "Loaded Configuration File" value in the output. This will tell you the location of your php.ini file.

  2. Open the php.ini file in a text editor.

  3. Find the "error_reporting" directive. It might be commented out with a semicolon (;). Remove the semicolon and set the desired error_reporting level, such as:

error_reporting = E_ALL
  1. Find the "display_errors" directive. Set it to "On" to display errors on the web page:
display_errors = On
  1. Save the changes and restart your web server to apply the new configuration.

Using PHP Script Configuration

If you don't have access to the php.ini file, you can also enable error reporting directly in your PHP script using the following lines of code:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>

Place these lines at the beginning of your PHP script before any other code. This will set the error_reporting level to E_ALL, which includes all types of errors, warnings, and notices, and display_errors to 1 (true), enabling error display.

Remember to remove or comment out these lines in your production environment to avoid exposing sensitive information to users.

0 Comment