PHP tries to be helpful when problems arise. It provides different types of error messages and warnings with as much information as possible. An understanding of errors will help you quickly recognize where problems exist in your code, which will lend to faster debugging and understanding where to look for problems.

Types of PHP Error Messages

PHP can display five types of messages. Each type of message displays the name of the file where the error was encountered and the line number where PHP encountered the problem.

  1. Notice: PHP sees a condition that might be an error or might be perfectly okay.
  2. Warning: PHP sees a problem, but the problem isn’t serious enough to prevent the script from running.
  3. Parse Error: A parse error is a syntax error that PHP finds when it scans the script before executing it.
  4. Fatal Error: PHP has encountered a serious error that stops the execution of the script.
  5. Strict: Strict messages, added in PHP 5, warn about coding standards.

Technically, notices are not errors. They notify things that you may have intended or wanted, but that PHP can do without. For example, using a variable on a page without declaring it first will generate a notice.

Warnings do not stop script from running, but indicate that something has gone wrong during execution of a script. Attempting to use include() on a file that does not exist would create a warning.

You get strict messages when you use language that is poor in coding practice or has been replaced by better code.

Displaying Error Messages

You can handle error messages in any of the following ways:

  • Display some or all error messages on your web pages
  • Don’t display any error messages
  • Suppress a single error message

You can tell PHP whether to display error messages or which error messages to display with settings in the php.ini file or with PHP statements in your scripts. Settings in php.ini set error handling for all your scripts.

To dictate what errors you do and do not wish to see in yout script output, you can use the error_reporting() function. By passing one or more of the constants, you can control what is reported.

// Turn off error reporting

// Report runtime errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);

// Report all errors

// Report all errors except E_NOTICE
error_reporting(E_ALL & ~E_NOTICE);

Turning Off Error Messages

Error messages are displayed on your web pages by default. Displaying error messages on your web pages is a security risk. You can have error messages turned on when you’re developing your website, so you can fix the errors, but when your web pages are finished and ready for the public to view, you should shut off the error messages.

display_errors = Off

You can turn off errors in an individual script with the following statements:


Logging Error Messages

You can store error messages in a log file. This produces a permanent record of the errors, whether or not they displayed on the web page.