PHP Errors
- Fatal error:
A severe problem
with the content of your program, such as calling a function that hasn't
been defined. The interpreter stops running your program when it
encounters a fatal error. Other examples: instantiating objects
of a non-existent class or using the break statement outside of
a loop.
- Warning:
An advisory from the
interpreter that something is fishy in your program, but the
interpreter can keep going. Using the wrong number of arguments when
you call a function causes a warning. Other examples:
MySQL connection problems, dividing by zero or including
a file that does not exist.
- Parse error:
A problem with the syntax of your program, such as
leaving a semicolon off of the end of
a statement. The interpreter stops running your program when it
encounters a parse error.
- Notice:
A tip from the PHP
interpreter, playing the role of Miss Manners. For example, printing a
variable without first initializing it to some value generates a
notice.
Constant | Description | Halts Script? |
E_ERROR | Fatal runtime error | yes |
E_CORE_ERROR | Fatal PHP startup error | yes |
E_COMPILE_ERROR | Fatal compile-time error | yes |
E_USER_ERROR | Fatal programmer generated error |
programmer defined |
| | |
E_WARNING | Non-fatal runtime error | no |
E_CORE_WARNING | Non-fatal PHP startup error | no |
E_COMPILE_WARNING | Non-fatal compile-time error | no |
E_USER_WARNING | Non-fatal programmer generated error | programmer defined |
| | |
E_PARSE | Compile-time parser error | yes |
| | |
E_NOTICE | Runtime notice | no |
E_USER_NOTICE | Programmer notice | programmer defined |
| | |
E_ALL | All of the above | |
Typically, once code is deployed, the only problems that
the code needs to handle are E_WARNING, E_USER_ERROR,
E_USER_WARNING and E_USER_NOTICE.
- Notice how the E_WARNING is displayed
- Notice how the @ symbol suppresses the message.
- Notice the error_reporting() method.
Debugging Tips
- View the source of html pages.
- Validate html pages.
- Use the PHP
flush()
method to force all output to the browser. However,
flush() should only be used for debugging as it makes PHP
buffering less efficient.
- Be careful with variable names since PHP is case sensitive.
- Detect uninitialized variables by using
the error_reporting(E_ALL); command.
Here is an example: debug1.php.
- Look for missing semicolons, braces or quotes on
the line above the error message.
Here is an example: debug2.php.
- Make sure that a script is properly opened and closed.
- Make sure that your code has the correct suffix.
Custom Error Handling
Production Error Handler Characteristics
- Should provide significantly more information than
the PHP internal error handler.
- Should not show technical information to a user
(a security risk), just explain that an error has
occurred.
- Should create a log file of technical information
for an administrator to review. This can be done
in PHP by using the
error_log()
method.
- Should handle programmer generated errors. A programmer
can throw an error using the
trigger_error()
method.
- Should delete session variables, close database
connections, unlock any locked database tables,
logout the user, etc., if this is warranted.
Laboratory
Do something interesting and non-trivial that involves
errors and error reporting.
If you are a Montana State student, demonstrate it during
today's lab period. If you are a University of Leipzig student,
either demonstrate it during today's lab period or e-mail it to
Sebastian (loewe AT uni-leipzig.de) no later than the end of the day.