Learn how to use PHP error_reporting Function

Introduction

Here, On this Page, I'm Publishing Some information about error_reporting function in PHP including Definition, Syntax, Usage, Examples, Parameter Constants, Return Output and some related details associated with it. PHP error_reporting function simplifies our work of changing default behavior of "PHP Errors" directly from our scripts without editing the core "php.ini" configuration file. This function is a part of core "error_handling functions" in PHP which allows us to define our own error handling rules suiting our needs.

Definition and Usage

The error_reporting function allows us set levels of error (in PHP) to be displayed on screen for the particular duration (runtime) of our script.

Syntax of error_reporting Function

int error_reporting ([ int $level ] );
php function syntax
php function syntax

In the above syntax,

  • First "int" indicates the data type of return value (function output),
  • Second "int" indicates the data type of parameter that this function will accept including predefined parameter constants.
  • $level is the parameter of error_reporting function which takes on either a bitmask or predefined named constants. As per the PHP official statement, named constants are suggested to ensure compatibility with future versions of PHP.

Latest Predefined Constants, Bitmasks and Description

Bitmasks
Named Constant
Short Description
1
E_ERROR
Fatal run-time errors. These indicate errors that can not be recovered from, such as a memory allocation problem. Execution of the script is halted.
2
E_WARNING
Run-time warnings (non-fatal errors). Execution of the script is not halted.
4
E_PARSE
Compile-time parse errors. Parse errors should only be generated by the parser.
8
E_NOTICE
Run-time notices. Indicate that the script encountered something that could indicate an error, but could also happen in the normal course of running a script.
16
E_CORE_ERROR
Fatal errors that occur during PHP's initial startup. This is like an E_ERROR, except it is generated by the core of PHP.
32
E_CORE_WARNING
Warnings (non-fatal errors) that occur during PHP's initial startup. This is like an E_WARNING, except it is generated by the core of PHP.
64
E_COMPILE_ERROR
Fatal compile-time errors. This is like an E_ERROR, except it is generated by the Zend Scripting Engine.
128
E_COMPILE_WARNING
Compile-time warnings (non-fatal errors). This is like an E_WARNING, except it is generated by the Zend Scripting Engine.
256
E_USER_ERROR
User-generated error message. This is like an E_ERROR, except it is generated in PHP code by using the PHP function trigger_error().
512
E_USER_WARNING
User-generated warning message. This is like an E_WARNING, except it is generated in PHP code by using the PHP function trigger_error().
1024
E_USER_NOTICE
User-generated notice message. This is like an E_NOTICE, except it is generated in PHP code by using the PHP function trigger_error().
2048
E_STRICT
Enable to have PHP suggest changes to your code which will ensure the best interoperability and forward compatibility of your code.
4096
E_RECOVERABLE_ERROR
Catchable fatal error. It indicates that a probably dangerous error occured, but did not leave the Engine in an unstable state. If the error is not caught by a user defined handle (see also set_error_handler()), the application aborts as it was an E_ERROR.
8192
E_DEPRECATED
Run-time notices. Enable this to receive warnings about code that will not work in future versions.
16384
E_USER_DEPRECATED
User-generated warning message. This is like an E_DEPRECATED, except it is generated in PHP code by using the PHP function trigger_error().
30719
E_ALL
All errors and warnings, as supported, except of level E_STRICT.

Return Output/Values

  • Returns the old error_reporting level or the current level if no "level" parameter is given.

Example of error_reporting Function

<?php

// Turn off all error reporting
error_reporting(0);

// Report simple running errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);

// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings ...)
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

// Report all errors except E_NOTICE
// This is the default value set in php.ini
error_reporting(E_ALL ^ E_NOTICE);

// Report all PHP errors (see changelog)
error_reporting(E_ALL);

// Report all PHP errors
error_reporting(-1);

// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);

?> 

Rate this Article ???

  • 1 Star
  • 2 Stars
  • 3 Stars
  • 4 Stars
  • 5 Stars
See results without voting

Tip

  • Passing in the value -1 as parameter will show every possible error, even when new levels and constants are added in future PHP versions.

More by this Author


Comments 1 comment

Joe 4 years ago

Heres a cool tool for setting PHP error_reprting: http://www.bx.com.au/tools/ultimate-php-error-repo...

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working