PHP Error Handling

What is Error Handling?

So, lets say you just completed writing some PHP code, and go to your browser to see the fruits of your labor, and instead you get a blank white page. This is what is commonly referred to as the "White Screen of Death," and it is often result of a mistake you made in your PHP code. But what mistake and where? A blank, white screen is not very helpful. If only there was a way to get more information about what went wrong. Well, fortunately there is. PHP has build-in error reporting and other function to aid you to find and fix errors, all you have to do now when and how to use them.

Type of PHP Errors

There are 5 categories of errors that PHP will report and display. They are Fatal Errors, Parse Errors, Warnings, Notices, and Deprecated.

Fatal Runtime Errors

Fatal runtime errors will kill the page completely. Common fatal runtime errors include running out of memory.

Parse Errors

Parse errors or syntax errors occur during compile time, and therefore occur before anything is sent to the page.

Warning and Notices

Warnings and notices are not as critical as errors and typical will not kill the page, but are an indication of poorly written code.

Deprecated

Is a notice that a method or statement being used will be removed from PHP in future versions and updated or replaced.

For beginners, the most common PHP error is the parse or syntax error. Fortunately, these are also the easiest to find and fix. The next examples what tools are available to find and fixes parse errors.

Finding and fixing parse errors

Common PHP Mistakes

Below is a list of common syntax mistakes.

  • Typos
  • Missing semicolon (;) at the end of line
  • Missing closing braces: } ) ]
  • Missing closing quote: " '
  • Misspelled variable name or mismatched case. PHP is case-sensitive.

While these errors are easy to fix that can be difficult to find. Fortunately, there several tools to help you with this problem. The first on is using a linter.

Using PHP Linter for Visual Studio Code

Visual Studio Code uses the official PHP Linter, out of the box, to provide live code feedback. This means has you type Visual Studio Code will provide highlighting and error messages if a syntax error is made.

View the documentation to learn more about PHP Linting with Visual Studio Code

Using PHP Error Reporting and Display Errors

Another method of find syntax errors it use PHP built-in error reporting and display those errors directly on the page. While these options comes built-in to PHP, they are often disabled by default. However, they are by default when using Local by Flywheel, so no additional configuration is needed.

With error reporting and display errors activated, when a syntax error occurs a message will be displayed in the browser instead of a blank page. This message will give a vague description of what went wrong along with a line number of the error. Just like the linter, the actually mistake is usually on the line before the line reported.

TIP

While it may seem redundant, it is actually very useful to use error reporting and a PHP linter. Linters typically only look for parse or syntax errors, while error reporting will caught other types of errors like warnings, notices, and deprecated.

Viewing the PHP Error Logs with Local by Flywheel

Viewing the PHP Error Logs created by Local by Flywheel is a straight forward process. In the File Explorer (Windows) or Finder (macOS), follow the site path of your Local Site. From your site path folder, the PHP error logs can be found in logs/php/7.2.0/error.log.

Finding logical errors

While some of these coding mistakes are syntax errors and easily identifiable by PHP Lint or Error Reporting, some are logical errors. Logical may not trigger any error or warning, but will still produce an undesirable result. They can also be difficult to find, especially for beginners. Just like with error reporting, the best way to find a logic error is to output information to the HTML page as the code is running. There are three common PHP statements and functions often used to output information to the page. They are echo, print_r(), and var_dump().

echo

You should already be familiar with echo. It is a way of displaying strings, numbers and variables to the page.

$string = "I am a string";
echo $string; // I am a string

However, if the variable's value is an array, the echo statement will not return the values in the array. Instead will either display the string "Array", which is not all that helpful, or cause a PHP error, which is worse.

$array = ["I am an array", "I have many values"];
echo $array // Array

This is where print_r() function comes in.

The print_r() function displays human-readable information about a variable, and while it can be use on with strings and number, it more commonly used to display arrays.

$array = ["I am an array", "I have many values"];
print_r($array) // Array ([0] => I am an array [1] => I have many values)

By using the print_r() function we can see all the value being stored in the array. When combined with the HTML <pre> tags, the output of the print_r() function when be neatly formatted when displayed.

var_dump

The var_dump() function take print_r to the next level, by providing even more information about variables include they current type (string, number, array, etc) and the size of the variable (number characters in the string, number items in the array). The var_dump() function also display arrays in a nice, human readable format.

$array = ["I am an array", "I have many values"];
var_dump($array) // array(2) {[0] => string(13) "I am an array" [1] => string(18) "I have many values"}

Error reporting will display other problems with your code beyond just syntax errors. In fact, there are many types of errors, with different error codes. Being knowledgeable what each code means will help you find and fix the issue.

Graceful Handling of Fatal Errors

Sometimes it is necessary to let the end user know an error has occurred, without displaying the error reporting messaging. This can be done using the register_shutdown_function() and error_get_last() functions. Together, these functions can be used to create and display a custom error message, as shown below.

WARNING

This will not work if there is a syntax error on the page.

<?php 
register_shutdown_function(function() {
  $error = error_get_last();
  if(!empty($error)) {
    echo "Huston, we have a problem.";
  }
}
?>