InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Tuning Error Handling in PHP
PHP

Tuning Error Handling in PHP

InfinityCoder December 23, 2016

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

To adjust the types of errors PHP complains about, use error_reporting():

1
2
3
error_reporting(E_ALL);                // everything
error_reporting(E_ERROR | E_PARSE);    // only major problems
error_reporting(E_ALL & ~E_NOTICE);    // everything but notices

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:

1
2
3
4
5
6
7
8
9
10
// 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:

1
2
3
4
5
6
7
8
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 20-2.
Table 20-2. Error types

Value Constant Description Catchable
1

2

4

8

16

32

64

128

256

512

1024

2048

4096

8192

16384

32767

E_ERROR

E_WARNING

E_PARSE

E_NOTICE

E_CORE_ERROR

E_CORE_WARNING

E_COMPILE_ERROR

E_COMPILE_WARNING

E_USER_ERROR

E_USER_WARNING

E_USER_NOTICE

E_STRICT

E_RECOVERABLE_ERROR

E_DEPRECATED

E_USER_DEPRECATED

E_ALL

Nonrecoverable error

Recoverable error

Parser error

Possible error

Like E_ERROR but generated by the PHP core

Like E_WARNING but generated by the PHP core

Like E_ERROR but generated by the Zend Engine

Like E_WARNING but generated by the Zend Engine

Like E_ERROR but triggered by calling trigger_error()

Like E_WARNING but triggered by calling trigger_error()

Like E_NOTICE but triggered by calling trigger_error()

Runtime notices in which PHP suggests changes to improve code quality (since PHP 5)

Dangerous error (such as mismatched type hint) but not fatal

Warning that you’ve used a deprecated function or feature

Deprecation warning you can trigger in your code

Everything

No

Yes

No

Yes

No

No

No

No

Yes

Yes

Yes

N/A

Yes

Yes

Yes

No

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.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Randomizing All Lines in a File in PHP
You want to randomly reorder all lines in a file. …

Randomizing All Lines in a File in PHP

Fetching a URL with an Arbitrary Method and POST Body in PHP
You want to request a URL using any method, such …

Fetching a URL with an Arbitrary Method and POST Body in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap