title besides title

 

Saturday, December 1, 2012

PHP : Web Basics - [8.16] Tuning Error Handling

8.16.1 Problem

You want to alter the error-logging sensitivity on a particular page. This lets you control what types of errors are reported.

8.16.2 Solution

To adjust the types of errors PHP complains about, use error_reporting( ):
error_reporting(E_ALL);                // everything
error_reporting(E_ERROR | E_PARSE);    // only major problems
error_reporting(E_ALL & ~E_NOTICE);    // everything but notices

8.16.3 Discussion

Every error generated has an error type associated with it. For example, if you try to array_pop( ) a string, PHP complains that "This argument needs to be an array," since you can only pop arrays. The error type associated with this message is E_NOTICE, a nonfatal runtime problem.
By default, the error reporting level is E_ALL & ~E_NOTICE, which means all error types except notices. The & is a logical AND, and the ~ is a logical NOT. However, the php.ini-recommended configuration file sets the error reporting level to E_ALL, which is all error types.
Error messages flagged as notices are runtime problems that are less serious than warnings. They're not necessarily wrong, but they indicate a potential problem. One example of an E_NOTICE is "Undefined variable," which occurs if you try to use a variable without previously assigning it a value:
// Generates an E_NOTICE
foreach ($array as $value) {
    $html .= $value;
}

// Doesn't generate any error message
$html = '';
foreach ($array as $value) {
    $html .= $value;
}
In the first case, the first time though the foreach, $html is undefined. So, when you append to it, PHP lets you know you're appending to an undefined variable. In the second case, the empty string is assigned to $html above the loop to avoid the E_NOTICE. The previous two code snippets generate identical code because the default value of a variable is the empty string. The E_NOTICE can be helpful because, for example, you may have misspelled a variable name:
foreach ($array as $value) {
    $hmtl .= $value; // oops! that should be $html
}

$html = ''
foreach ($array as $value) {
    $hmtl .= $value; // oops! that should be $html
}
A custom error-handling function can parse errors based on their type and take an appropriate action. A complete list of error types is shown in Table 8-2.

Table 8-2. Error types
Value
Constant
Description
Catchable
1
E_ERROR
Nonrecoverable error
No
2
E_WARNING
Recoverable error
Yes
4
E_PARSE
Parser error
No
8
E_NOTICE
Possible error
Yes
16
E_CORE_ERROR
Like E_ERROR but generated by the PHP core
No
32
E_CORE_WARNING
Like E_WARNING but generated by the PHP core
No
64
E_COMPILE_ERROR
Like E_ERROR but generated by the Zend Engine
No
128
E_COMPILE_WARNING
Like E_WARNING but generated by the Zend Engine
No
256
E_USER_ERROR
Like E_ERROR but triggered by calling trigger_error( )
Yes
512
E_USER_WARNING
Like E_WARNING but triggered by calling trigger_error( )
Yes
1024
E_USER_NOTICE
Like E_NOTICE but triggered by calling trigger_error( )
Yes
2047
E_ALL
Everything
n/a
Errors labeled catchable can be processed by the function registered using set_error_handler( ) . The others indicate such a serious problem that they're not safe to be handled by users, and PHP must take care of them.