Web Development 4 min read

PHP7 Error Reporting (a 2019 developers’ guide)

Written on 26 Jul 2019

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.

PHP error reporting

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.
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
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
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?
PHP Developer Jobs in Kolkata

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.
ConstantBitmask ValueMeaning
E_ERROR1The big one. Fatal runtime error, total and immediate program crash.
E_WARNING2The vast majority of errors you see will be warnings. They’re nonfatal errors that don’t prevent the program from executing.
E_PARSE4This happens when there’s a mistake in the syntax of the code it’s trying to parse.
E_NOTICE8Less 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_ERROR16Fatal runtime crash, but generated by the PHP core during initial startup.
E_CORE_WARNING32Nonfatal startup error generated by the PHP core.
E_COMPILE_ERROR64What happens when the Zend framework encounters a fatal error during compiling.
E_COMPILE_WARNING128Nonfatal Zend compiling error.
E_USER_ERROR256An intentional fatal error set off by the developer using the trigger_error() string.
E_USER_WARNING512Nonfatal 256.
E_USER_NOTICE1024A softer 512.
E_STRICT2048This 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_ERROR4096A 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_DEPRECATED8192PHP 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_DEPRECATED16384As 8192, but set off using the trigger_error() string.
E_ALL32676All 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.1k reads

Similar Reads