PHP Error Handling Tuning Error Handling - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP Error Handling Tuning Error Handling - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Tuesday, July 2, 2019

PHP Error Handling Tuning Error Handling

PHP Error Handling 


Tuning Error Handling

Problem

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

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

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” because 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.inirecommended configuration file sets the error reporting level to E_ALL, which is all error types.

PHP 5.0 introduced a new error level, E_STRICT. Enabling E_STRICT during development has the benefit of PHP alerting you of ways your code could be improved. You will receive warnings about the use of deprecated functions, along with tips to nudge you in the direction of the latest and greatest suggested methods of coding. For PHP 5.0–5.3, E_STRICT is the only error level not included in E_ALL; for maximum coverage during development, set the error reporting level to E_ALL | E_STRICT. Starting with PHP 5.4, E_STRICT is included in E_ALL.

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 through 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.

Table Error types

_____________________________________________________________________________________________________

______________________________________________________________________

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.

The E_RECOVERABLE_ERROR type was introduced in PHP 5.2.0. The E_DEPRECATED and E_USER_DEPRECATED types were introduced in PHP 5.3.0.


No comments:

Post a Comment

Post Top Ad