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.
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.
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.
A parse error is a fatal error, preventing the script from even starting to run. Before starting to run a script, PHP scans the script for syntax errors. When it encounters an error, it displays a parse error message.
A fatal error message is displayed when PHP encounters a serious error during the execution of the script that prevents the script from continuing to execute. For example, If you misspell a function name in your PHP script, you get a fatal error message.
You get strict messages when you use language that is poor in coding practice or has been replaced by better code. Some of the strict messages refer to PHP language features that have been deprecated. Deprecated functions are old functions that have been replaced by newer functions. The deprecated functions are still supported, but will be removed in the future.
You can handle error messages in any of the following ways:
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 are 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.
In php.ini file:
display_errors = Off
You can turn off errors in an individual script with the ini_set() function:
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.
By default, PHP will output the error messages to user output. The application errors should be treated in such a way that the end user will not see some possible faults when they occur, or user-friendly error messages pertaining to the operation that it was not possible to execute.
Default Error Handler
PHP uses a default error handler, provided no other error handler is specified by the developer, that simply outputs the error message to the user output. This message contains the error message itself, the filename, and the line number where the error was triggered.