Join Jon Peck for an in-depth discussion in this video Configuring PHP to report what went wrong, part of Debugging PHP: Advanced Techniques.
In PHP there are two configuration options that control what errors are reported and whether these errors are displayed. Depending on the platform or software distribution that is being used, the initial configuration of your system may be different from the default, so I'll describe how to both get and set these options. Error reporting sets the error reporting level with an integer representing a bit field normally through a named constant. By default, PHP reports everything except E_NOTICE and for versions prior to PHP 5.4, also excluding E_STRICT.
Display_errors determines whether or not to display errors to the user. It's either 1 (on) or 0 (off). By default, display_errors is set to 1 (on), meaning errors are shown to the user. Display_errors should only be set to 1 (on) for development servers and never shown to users, as this both confuses unskilled users and provides context for malicious users. Error logging will be covered in-depth in the next segment. There are four places where error_reporting and display_errors can be set in order of increasing priority and granularity.
PHP.ini, which is the PHP configuration file, which requires a server restart and affects the entire server; httpd.conf, this is the Apache server configuration file and editing requires a server restart as well. It also requires to allow override options or allow override all privileges; htaccess, which are directory level configuration files for PHP does not require a server restart, but isn't always available and also requires allow override options or allow override all privileges.
And finally, run time, which is in the script itself and it's always available. Now depending on the level of administrative access available, it may be possible that the only option available is run time configuration. However, there are some configuration options that cannot be changed at run time, so access to php.ini is preferred. I will demonstrate how to edit php.ini and to set these options at run time, but not the other configuration files out of interest of focusing on PHP and not Apache server configuration.
The first method I will demonstrate is editing php.ini, which will arbitrarily set the configuration for every site using that Web server. On a development server, this is optimal and easy, but this technique may not be appropriate in all circumstances. Use your best judgment. To set error_report and display_errors in php.ini, I need to first locate the configuration file. Similar to how defaults maybe different across distributions and platforms, the configuration file location can be found in a number of different places. Some solution stacks such as MAMP actually generate the php.ini file, so consult the documentation if the following technique is not effective.
The easiest way to determine the location of the configuration file is using the function phpinfo() which outputs information on the PHP configuration. I'm going to switch to my IDE NetBeans and open the demo.php file from the exercise files. I'll add one line, phpinfo, save, then go to the browser and navigate to where the test script is. In my case, it's http.//sandbox.dev.8080.
A large amount of debugging information is shown. I'm looking for loaded configuration file, which contains the complete path of the php.ini file. In my environment, I can see that the path is set to etcphp5apache2php.ini. Now that I know where the file is, I can edit it. As I'm using a virtualized development server, I'm going to SSH into it then edit the file. If you are not using a virtualized server or you are using a hosted server or solution stack, then the method used to edit the file and restart the Apache Web server will be slightly different, but the contents of the file will be very similar.
First, I'll open a console then I will SSH into the development environment. Next, I will edit the configuration file shown by phpinfo using administrative credentials, sudo nano -w and then the file. I will Search for the line containing error_reporting =. This is one of the lines that I edited in the Up and Running with Linux for PHP Developers, where I set the configuration to every single notice, warning, and error and added strict notices as well.
Above this line are a few suggested configurations for different environments, including the default value and production, if not already set, change the error_reporting to E_ALL | E_STRICT. Next, Search for display_errors, display_errors =, by default it's set to on, if not, change it to on. Exit by pressing Ctrl X and press Y if you changed anything. Finally, if there are changes that need to take place, I will need to restart the Apache Web server in order to allow the new PHP configuration options to be read.
This will differ from server to server, but the most common way to do this and the way used for the virtualized development server is to issue the following command, sudo /etc/init.d/apache2restart. Now, I'll go back to the IDE. As I now know where the configuration setting is, I no longer need the phpinfo function call, so I'll remove it. If you were unable to locate the configuration file or do not have permission to change it, I will now demonstrate how to set these values at run time in the script itself.
A caveat, if display_errors is set at run time, it won't have any effect if the script has a fatal error before execution, as the desired run time action does not get executed. Additionally, if you are using a third-party host, they may have disabled the use of the function I will be demonstrating for security reasons. If so, find an alternative such as a local development server that you directly administer. First, I'm going to set the error_reporting level using the function error_reporting, which takes one parameter, the integer value for the bit mask, which I will use constants to create.
I'll use the same value as I did in php.ini. I'll start with the comment, // Set error_ reporting, error_reporting (E_ALL | E_STRICT). There are two functions that I will use to interact with PHP options, ini get and ini set. Ini get returns the value currently stored for a given configuration option, echo <h1>display_errors: before</h1> and var_dump (ini_get('display_errors'));.
Next, I'm going to intentionally trigger a notice by attempting to access as a variable that does not exist, echo <h1>Triggering notice</h1>; then var_dump ($error); which does not exist. I'm going to explicitly disable the display of errors using the function ini_set to override the php.ini option. Ini_set takes two parameters, the name of the option to be set and the new value.
So echo<h1>Setting errors</h1>; then ini_set ('display_errors', 0). I'll verify the new setting has been saved; I can copy and paste this code and then attempt to trigger the notice again. Save, then return to the browser and refresh. The first display_errors shows that it was set to 1 by the php.ini.
When I triggered a notice by accessing error, the undefined variable, it is displayed to the screen followed by the result of var_dump, which correctly returned to null. Next, the ini_set function call, which does not display anything when called. The value for display_errors is now set to 0 and therefore the attempt to trigger a notice fails. Let's simplify the file a bit. We'll continue not to display errors. Do not display_errors. Finally, to reiterate a point from earlier, using ini_set to control display_errors will only work if the script actually executes.
If there is a fatal error, the default value set in php.ini will be used instead. So if I just do error, this is an invalid line, save then return to the browser and refresh. Despite attempting to disable error display, a parse syntax error is displayed, because the run time action was not executed. Keep this in mind when configuring production servers in preparing code for production. I am going to switch back to the IDE and clean up the demo file to prepare for the rest of the course. So I'm going to Display_errors and then I'm going to intentionally echo <h1>Triggering notice</h1>; var_dump ($error); and save.
Now that I've set error_reporting to a development finding value and I'm displaying errors to the screen, I'll explore how to log errors to a file for analysis.
This course covers installation of Xdebug on Apache as well as working with the NetBeans IDE (integrated development environment). Jon then introduces native web browser developer tools for Firefox and Chrome, and demonstrates browser independent web debugging tools. Best practices for debugging and profiling web application failures and performance issues are also covered.
- Configuring PHP error reporting
- Logging errors to file
- Gracefully handling fatal errors
- Installing Xdebug
- Understanding the principles of remote debugging
- Remote debugging with NetBeans
- Extending your browser with Firebug, FirePHP, or ChromePHP