Displaying PHP Errors


PHP provides remarkably useful and descriptive error messages when things go awry. Unfortunately, PHP doesn't show these errors when running using its default configuration. This policy makes sense for live servers, where you don't want the end users seeing PHP-specific error messages, but it also makes everything that much more confusing for the beginning PHP developer. To see PHP's errors, you must turn on the display_errors directive, either in an individual script or for the PHP configuration as a whole.

To turn on display_errors in a script, use the ini_set() function. As its arguments, this function takes a directive name and what setting it should have:

 ini_set('display_errors', 1); 

Including this line in a script will turn on display_errors for that script. The only downside is that if your script has a syntax error that prevents it from running at all, then you'll still see a blank page. To have PHP display errors for the entire server, edit your php.ini file, assuming you can (unless you host your site on your own computer, it probably won't be an option). If you don't yet know where to find the php.ini file, see the "Configuring PHP" section of Appendix A.

To turn on display_errors

1.

Open the active php.ini file in any text editor.

2.

Find the line that reads

 display_errors = Off 

Note that the php.ini file will include this line twice. The first time, it's just a reference and is preceded by a semicolon, meaning that the line is not active. Find the line that does not begin with a semicolon.

3.

Change the line to read

 display_errors = On 

4.

Save the php.ini file.

5.

Restart your Web server application.

You do not have to restart the entire computer, just the Web serving application (Apache, IIS, etc.). How you do this depends upon the Web server being used, the operating system, and the installation method. See Appendix A for instructions.

6.

If you want, rerun a phpinfo.php script to confirm that the change was made (Figure 6.8).

Figure 6.8. My phpinfo.php page now reflects that display_errors is On.


Again, the first appendix discusses the phpinfo.php script if you don't already have one.

Tips

  • While in the php.ini file, you can adjust other directives. For example, the log_errors and error_log settings control whether and to where errors are logged. If log_errors is On, a text file will store all of the problems that occur. You probably don't want this during developmentwhen lots of problems existbut do for a live server.

  • If your php.ini file has track_errors set to 1 (or on), every error will auto-matically be stored in an array called $php_errormsg.

  • The ini_set() function cannot be used to adjust every possible PHP setting. See the PHP manual for specifics as to what can and cannot be controlled.

  • The php.ini file includes pretty good instructions as to what the most relevant settings are, how you would change them, and why you might not want to. Pay attention to these instructions and perhaps make a backup copy of the file before editing it (just in case).




    PHP and MySQL for Dynamic Web Sites. Visual QuickPro Guide
    PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (2nd Edition)
    ISBN: 0321336577
    EAN: 2147483647
    Year: 2005
    Pages: 166
    Authors: Larry Ullman

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net