A PHP application produces many levels of errors during the runtime of the script . So in this article, we will learn how to display all the errors and warning messages. Show The quickest way to display all php errors and warnings is to add these lines to your PHP code file: ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); The ini_set function will try to override the configuration found in your php.ini file. If in the php.ini file display_error is turned off it will turn that on in the code. It also set display_startup_errors to true to show the error message. error_reporting() is a native PHP function that is used to display the errors. By setting it true it displays the error that occurs in the code. But there is an again question arises what is E_ALL? The answer is simple PHP code produces different levels of errors. So let's learn what are those types of errors occurs in a PHP code.
Updated on 30-Jul-2019 22:30:26
I still vividly remember the first time I learned about PHP error handling. It was in my fourth year of programming, my second with PHP, and I'd applied for a developer position at a local agency. The application required me to send in a code sample (GitHub as we know it didn't exist back then) so I zipped and sent a simple custom CMS I'd created the previous year. The email I got back from the person reviewing the code still chills my bones to this
day. "I was a bit worried about your project, but once I turned error reporting off, I see it actually works pretty well". That was the first time I searched "PHP error reporting", discovered how to enable it, and died inside when I saw the stream of errors that were hidden from me before. PHP errors and error reporting are something that many developers new to the language might miss initially. This is because, on many PHP based web server installations, PHP errors may be
suppressed by default. This means that no one sees or is even aware of these errors. For this reason, it's a good idea to know where and how to enable them, especially for your local development environment. This helps you pick up errors in your code early on. If you Google "PHP errors" one of the first results you will see is a link to the error_reporting function documentation. This
function allows you to both set the level of PHP error reporting, when your PHP script (or collection of scripts) runs, or retrieve the current level of PHP error reporting, as defined by your PHP configuration. The There is a long list of possible values you can pass as a parameter, but we'll dive into those later. For now it's important to know that each possible value also exists as a PHP predefined constant. So for example, the constant As a quick example, if we create a
Error reporting configurationUsing the But it would be better to control which errors are being reported on in your local development environment, and log them somewhere logical, to be able to review as you code. This can be done inside the PHP initialization (or The If you're not sure where your
As you can see from my phpinfo, my current Once you've found your Error reporting directivesThe first thing you'll see in that section is a section of comments which include a detailed description of all the Error Level Constants. This is great, because you'll be using them later on to set your error reporting levels. Fortunately these constants are also documented in the online PHP Manual, for ease of reference. Below this list is a second list of Common Values. This shows you how to set some commonly used sets of error reporting value combinations, including the default values, the suggested value for a development environment, and the suggested values for a production environment.
Finally, at the bottom of all the comments is the current value of your
This is usually one of the first things I set when I set up a new development environment. That way I'll see any and all errors that are reported. After the error_reporting directive, there are some additional directives you can set. As before, the php.ini file includes descriptions of each directive, but I'll give a brief description of the important ones below. The
The The The rest of the directives can be left as the default, except for maybe the Custom error loggingI use the Apache web server on Ubuntu, and my project-specific virtual host configurations use the following to determine the location for the error log.
This means it will log to the default Apache log directory, which is So, depending on your local development environment you may need to tweak this to
suit your needs. Alternatively, if you can't change this at the web server level, you can set it at the
It is worth noting that this will log all PHP errors to this file, and if you're working on multiple projects that might not be ideal. However, always knowing to check that one file for errors might work better for you, so your mileage may vary. Find and fix those errorsIf you've recently started coding in PHP, and you decide to turn error reporting on, be prepared to deal with the fallout from your existing code. You may see some things you didn't expect, and need to fix. The advantage though, is now that you know how to turn it all on at the server level, you can make sure you see these errors when they happen, and deal with them before someone else sees them! Oh, and if you were wondering, the errors I was referring to at the start of this post were related to the fact that I was defining constants incorrectly, by not adding quotes around the constant name.
PHP allowed (and might still allow) this, but it would trigger a notice.
This notice was triggered every time I defined a constant, which for that project was about 8 or 9 times. Not great for someone to see 8 or 9 notices at the top of each page... Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started How do I fix PHP errors?Editing the php.. Log into your cPanel.. Go to the File Manager. ... . Find the “Error handling and logging” section in the php.ini. ... . Next you can set the display_errors variable to On or Off to either show the errors on your website or not.. Does PHP support error reporting?The error_reporting() function specifies which errors are reported. PHP has many levels of errors, and using this function sets that level for the current script.
How do I view PHP errors in Chrome?How do I view PHP errors in Chrome ?. E_ALL. error_reporting(E_ALL); By E_ALL error level, it will report all errors to the browser or chrome in your case specifically.. E_NOTICE. error_reporting(E_NOTICE); ... . E_ERROR. error_reporting(E_ERROR ); ... . E_WARNING. error_reporting(E_WARNING ); ... . E_PARSE. error_reporting(E_PARSE );. Where does PHP log errors by default?By default, whenever an error or exception is thrown, PHP sends the error message directly to the user via STDOUT. In a command-line environment, this means that errors are rendered in the terminal. In a web environment, errors and exceptions get displayed directly in the browser.
|