How do i display php errors?

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 error_reporting function accepts a single parameter, an integer, which indicates which level of reporting to allow. Passing nothing as a parameter simply returns the current level set.

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 E_ERRORhas the value of 1. This means you could either pass 1, or E_ERROR to the error_reporting function, and get the same result.

As a quick example, if we create a php_error_test.php PHP script file, we can see the current error reporting level set, as well as set it to a new level.

Bài mới nhất

Chủ Đề