Posted by & filed under php.

There’s a way to hide those ugly error outputs from PHP, and not just that you can track them, log them and manage them.

We have different type of errors some of them are possible to catch and others aren’t, in this post you will learn how to build your own error tracker, but first lets learn something about PHP errors, check the different type of errors in the following list.

Value Constant Description PHP
1 E_ERROR Fatal run-time errors. Errors that cannot be recovered from. Execution of the script is halted
2 E_WARNING Non-fatal run-time 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. The script found something that might be an error, but could also happen when running a script normally
16 E_CORE_ERROR Fatal errors at PHP startup. This is like an E_ERROR in the PHP core 4
32 E_CORE_WARNING Non-fatal errors at PHP startup. This is like an E_WARNING in the PHP core 4
64 E_COMPILE_ERROR Fatal compile-time errors. This is like an E_ERROR generated by the Zend Scripting Engine 4
128 E_COMPILE_WARNING Non-fatal compile-time errors. This is like an E_WARNING generated by the Zend Scripting Engine 4
256 E_USER_ERROR Fatal user-generated error. This is like an E_ERROR set by the programmer using the PHP function trigger_error() 4
512 E_USER_WARNING Non-fatal user-generated warning. This is like an E_WARNING set by the programmer using the PHP function trigger_error() 4
1024 E_USER_NOTICE User-generated notice. This is like an E_NOTICE set by the programmer using the PHP function trigger_error() 4
2048 E_STRICT Run-time notices. PHP suggest changes to your code to help interoperability and compatibility of the code 5
4096 E_RECOVERABLE_ERROR Catchable fatal error. This is like an E_ERROR but can be caught by a user defined handle (see also set_error_handler()) 5
6143 E_ALL All errors and warnings, except of level E_STRICT 5

The error_reporting function has a parameter to set which type of error you want to show.

You can set to 0 if you don’t want to show nothing

To show the common errors

If you’re picky , this will help you to code better

This is the default set from php.ini

This show everything

I use this, I care more about these ones

You need to create a new PHP file that’s going to handle the errors and you have to load it in every PHP file that runs by itself  I mean that is not loaded in another PHP (but don’t worry using require_once it will be loaded just once)…but well this depends of the structure of your project .

There’s a function called set_error_handler with this one you can catch many errors but  there are some E_FATAL errors that are impossible to catch with this function, so theres a trick to catch them and show something else instead.

So in your function you should classified which kind of error you are receiving

With this code from above you can send emails or log errors in Data Base.

The second function you need is the one it takes the fatal errors from the tags, and we show a custom error html.

Well if you use this code I fully recommend you to take all the possible variables that might be part of the error.
There’s a function call debug_backtrace that returns you the array of all the trace you code did. so I recommend you log it too.

Update: If you are using the PHP version 5.3 or earlier, now comes with the new error levels E_DEPRECATED & E_USER_DEPRECATED

11 Responses to “PHP Error Handler, Tracking you project bugs”

  1. WishCow

    Using E_ERROR and E_PARSE only, is very bad practice. This leads to a much harder debugging time, and gives a lot of headache for developers who are also working on the project, but use the suggested E_ALL | E_STRICT level.

  2. Benoit

    I totally agree with WishCow hiding warnings is a huge source of bugs. If they exists it’s for a good reason ! Sure an unexisting array would not lead every time to an error for the user, but for the maintanability of code I prefer write a little more and get a warning free code.

    I am currently enforcing my own practices using E_STRICT reporting too, it gaves good hint when working with full OOP applications (method overloading without the same parameters, etc.)

  3. pcdinh

    You can not handle the following error using userland custom error handler: E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING


  1.  nanning china
  2.  accountants in Sydney
  3.  Noleggio auto a Ibiza e Formentera
  4.  kids reading books
  5.  sponsor
  6.  lockerz free instant invite
  7.  gaggia accademia espresso machine review
  8.  wine countries
  9.  Pre workout supplements
  10.  Firemen jobs
  11.  Americas Got Talent
  12.  Ab Circle Pro Review
  13.  cool stuffs
  14.  Church chairs
  15.  Poker Blog
  16.  Buy Youtube Views
  17.  Glasgow
  18.  Ring Central Review