4.3 Setting Breakpoints by Using PHP s Error Constants


4.3 Setting Breakpoints by Using PHP's Error Constants

You want to set different levels of error checking in a script.

Technique

Use PHP's error_reporting() function in conjunction with the predefined error constants:

 <? error_reporting(E_WARNING); $line = "The\nQuick\nYoung\nProgrammer\nJumped\nOver\nThe\nLazy\nOld\          nProgrammer"; $line = ereg_replace("(\n"," ",$line); // mis-matched parentheses print $line; ?> 

Comments

In this code, the regular expression in the first argument of ereg_replace() has a mismatched parenthesis (the correct syntax would be ereg_replace("(\ n)"," ", $line);) . Normally, PHP would not report this error, but because we told the PHP processor through the error_reporting() function that we want PHP to report regular expression errors, we now get the error reported to us. E_WARNING is just one of the different levels of error reporting. The following is a table of all the error constants.

Constant Description
E_ALL Enables all errors.
E_ERROR Enables any error other than parsing from which recovery is not possible.
E_WARNING Denotes a condition where PHP knows something is wrong, but will continue anyway; these can be caught by the script itself. An example is the invalid regexp in ereg_replace() shown earlier.
E_PARSE The parser choked on invalid syntax in the script file. Recovery is not possible.
E_NOTICE Something happened that might not be an error, and execution continues. Examples include using an unquoted string as an associative array index, or accessing a variable that has not been set.

Using these constants in conjunction with the error_reporting() function will help you better debug your scripts and find major flaws and quirks that PHP might not be reporting, but that are causing your program to act differently. The default error-reporting level is E_ALL & ~E_NOTICE (all errors except notices).



PHP Developer's Cookbook
PHP Developers Cookbook (2nd Edition)
ISBN: 0672323257
EAN: 2147483647
Year: 2000
Pages: 351

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