Despite the early 2010’s doomsaying predictions, PHP is still dominating the web. The release of PHP7 has been a vital shot in the arm for the venerable client-side language, and it’s more important than ever to be able to write, understand and debug PHP. In this article, we’ll go over how to turn on error reporting, and how to understand what you’re looking at.
How to Report Errors in PHP7
To get the full range of error reporting options, you may need to edit your php.ini file. Without editing your php.ini file, you can usually get some of the error information by adding the following lines to the top of the php file you want a report on.
This won’t show any errors parsing the code, like improper formatting. That would simply cause a blank page since the entire script is parsed before the execution happens.
If you want to see these errors, you can add the following line to your php.ini file
display_errors = on
It is important to note that you should not leave this function on for a production server!
Another option is to edit your .htaccess configuration file, which may prevent you from accidentally carrying over the setting to your production server, or may be needed if your web server provider does not allow you to access your php.ini file in this way.
Add the following lines
php_flag display_errors on
php_flag display_startup_errors on
From here, you can have it output all errors to a log file, giving you feedback even when a page fails to load.
Add the following line
php_value error_log log/errors_php.log
You can name the file as you please, and place it in any path you want – the example path will be in a folder relative to where .htaccess is located.
Narrowing it down
The default error_reporting value for PHP 5.4 and up is
E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED
Which means by default, all errors are shown except E_NOTICE, E_STRICT, and E_DEPRECATED.
In the same way, you can specify which error levels you want by replacing the values with your chosen levels, separated by an inclusive OR, like this:
error_reporting(E_WARNING | E_PARSE);
You can also use AND NOT to show all errors except a certain type.
This line will report all errors except nonfatal runtime warnings.
error_reporting(E_ALL & ~E_WARNING);
Easy! Now you’ve got your reporting set up, what do the numbers actually mean?
PHP7 Predefined Constants and Bitmask Values
Below if a list of all of the options for specifying which errors to report. Not that the constant names will only work inside of php.ini or in a php file- if you are adding these to your .htaccess file, you must use the bitmask value.
Constant
Bitmask Value
Meaning
E_ERROR
1
The big one. Fatal runtime error, total and immediate program crash.
E_WARNING
2
The vast majority of errors you see will be warnings. They’re nonfatal errors that don’t prevent the program from executing.
E_PARSE
4
This happens when there’s a mistake in the syntax of the code it’s trying to parse.
E_NOTICE
8
Less than a warning, but still something that might merit attention. A lot of the time you’ll see these when everything fine, but sometimes they manage to catch a major issue before it has had time to metastasize.
E_CORE_ERROR
16
Fatal runtime crash, but generated by the PHP core during initial startup.
E_CORE_WARNING
32
Nonfatal startup error generated by the PHP core.
E_COMPILE_ERROR
64
What happens when the Zend framework encounters a fatal error during compiling.
E_COMPILE_WARNING
128
Nonfatal Zend compiling error.
E_USER_ERROR
256
An intentional fatal error set off by the developer using the trigger_error() string.
E_USER_WARNING
512
Nonfatal 256.
E_USER_NOTICE
1024
A softer 512.
E_STRICT
2048
This isn’t strictly an error, it’s just PHP warning you about code that might become deprecated and/or nonfunctional in future. No longer very useful in PHP7
E_RECOVERABLE_ERROR
4096
A fatal error that got caught by an error handler, leaving PHP still recoverable. If it doesn’t get caught, it turns into an E_ERROR.
E_DEPRECATED
8192
PHP letting you know that while a particular piece of code works fine for now, it will not be supported by future releases. STRICT is a soft maybe, and this is a hard no.
E_USER_DEPRECATED
16384
As 8192, but set off using the trigger_error() string.
E_ALL
32676
All errors supported.
Bringing it together
Say you want everything except warnings of every flavour, you could do the following:
And that’s it! That’s the basics of PHP error reporting. Good luck, and fingers crossed you don’t throw too many up. Is PHP your passion? Are you looking for work? We have PHP Developer Jobs in Kolkata as well as jobs for Laravel developers and CodeIgniter Experts. If this sounds like a good fit for you, check out our careers page. CodeClouds: Simply Brilliant!
Share this article
3.2k reads
Contents
1.Overview
2.How to Report Errors in PHP7
3.Narrowing it down
4.PHP7 Predefined Constants and Bitmask Values
5.Bringing it together
Similar Reads
How can we help?
Tell us about your inquiry and we’ll get back to you as soon as we can.
0
Message Sent
Thanks for contacting us, we’ll be in touch shortly!