power of error handling in PHP. Some developers I have
talked with recently, simply set error_reporting to 0 and
carry on regardless. If you cannot see the error, it doesnt
the purpose it was built. However, we do not want glaring
errors popping up in production website or application–
these could not only be embarrassing, they could be security
flaws. So what is it that we can do to stop errors from
first giving out sensitive server information, and second
killing our script half-way down the page? Luckily for us
PHP has several built-in error handlers for us to use. Let’s
have a look.
Warning: fopen(example.txt) [function.fopen]: failed to open stream:
No such file or directory in C:directoryfile.php on line 2
above, we test if the file exists before we try to access
die("File not found");
File not found
because it uses a simple error handling mechanism to stop
the script after the error. However, simply stopping the
script is not always the right way to go. Let’s take a look
at alternative PHP functions for handling errors.
create a special function that can be called when an error
occurs in PHP.
parameters (error level and error message) but can accept up
to five parameters (optionally: file, line-number, and the
error_function(error_level, error_message, error_file, error_line, error_context)
level for the user-defined error. Must be a value number.
error_message: Required. Specifies the error message for the user-defined error
error_file: Optional. Specifies the filename in which the error occurred
error_line: Optional. Specifies the line number in which the error occurred
error_context: Optional. Specifies an array containing every variable, and their values, in use when the error occurred