When developing PHP on a local computer it is usual to configure PHP to display error and warning messages. Enabling errors and warnings in PHP helps during software development to find bugs and detect potential problems.
This article explains how to enable PHP error and warning messages for local web development on a LAMP stack. Linux Mint is used as an example, but other Debian Linux computers such as Ubuntu should follow the same procedure.
Create a Test PHP File
Create a PHP file that contains an error. When PHP error and warning reporting is not enabled, and the PHP script is executed, no error or warning messages are displayed in the web browser.
Here is an example of a PHP script that contains an error.
<?php // generates PHP error // useful for testing if error reporting is on ech "<p>Hello, world!</p>";
Save this script to a file, e.g. php_error.php and copy it to a local web server. Use a web browser to surf to the script. If you configured a test website as explained in the article on configuring Apache for local web development, then you can copy the test file to your test folder. Use a web browser to surf to http://test/php_error.php
When the script is run, a blank page is appears in the web browser as shown in the above image.
Configure PHP to Display Errors and Warnings
After configuring PHP to display errors and warnings, the same test script created above will be used to verify that error reporting has been switched on.
Enable PHP Error and Warning Messages
First locate the php.ini configuration file in the file system. This file is located in /etc/php/7.0/apache2/ in Linux Mint 18. In older versions of Linux Mint it was located in /etc/php5/apache2/ If you can’t find php.ini, do a search for it in the /etc/ directory.
Open the php.ini file in an editor with superuser privileges by either starting file manager as root or using sudo from the command line. Choose one of the options below to open and edit php.ini
Open File Manager as Root
Press Alt + F2 to open the Run Application dialog box. Enter gksudo caja in the Run Application dialog box and press enter. Enter your password when prompted and an instance of the Caja file manager will open (Linux Mint Mate). If you are using a different distribution of Linux, substitute the name of your file manager for caja.
Use the file manager opened as root to navigate to the php.ini file, e.g. to /etc/php/7.0/apache2/ in Linux Mint 18. Double click the php.ini file to open it for editing.
Edit php.ini from the Command Line
Instead of using a file manager opened as root, php.ini can be opened from the command line. Start a terminal window (Ctrl + Alt + t). Now open the php.ini file in the nano file editor using the following command:
sudo nano /etc/php/7.0/apache2/php.ini
Change the path to php.ini in the above line if necessary.
With php.ini opened in an editor with root privileges, PHP error and warning messages can be enabled.
Scroll down the php.ini file to find the “Error handling and logging” section as shown below, or search for “Error handling and logging”.
Scroll down to find what error_reporting is set to. By default this value is set as follows on a Linux Mint 18 system.
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
For the strictest error reporting, and to report the use of deprecated PHP code, change this value to the following.
error_reporting = E_ALL | E_DEPRECATED | E_STRICT
Scroll a bit further down the file and change the value of display_errors to On.
display_errors = On
This will enable all PHP errors and warnings to be displayed when they occur.
Save the file and exit the editing program. (use Ctrl + o to save the file and then Ctrl + x to exit the application if using the nano editor).
Restart Apache Server
For the above changes to take effect, the Apache web server must be restarted. Enter the following at the command line.
sudo service apache2 restart
Testing that Error and Warning Reporting is Enabled
Use the same php_error.php file from above to test that the error in this file is reported. Use a browser to navigate to the file as done previously. An error should now be displayed in the browser as shown below.
When to Configure PHP to Display Errors and Warnings
PHP should only be configured to display errors and warnings on a local test sever. A live or production server should not display PHP error and warning messages. Code should first be tested and debugged on a local server. When it is error free it can be moved to the live production server.