PHP Error Handling
A side-note for the use of exit with finally: if you exit somewhere in a try block, the finally won't be executed. Could not sound obvious: for instance in Java you never issue an exit, at least a return in your controller; in PHP instead you could find yourself exiting from a controller method (e.g. In case you issue a redirect). The most effective method of fixing this vulnerability is to add die or exit after the Location header for PHP to stop processing the rest of the page. The only difference between die and exit is that die closes the HTTP connection (see the Connection HTTP header below), while exit does not (from a security perspective they are.
We already know types of errors in out previous PHP tutorial. In this PHP tutorial we will learn how to handle errors in php.
An error is a unwanted result of program. Error is a type of mistake.
- Definition and Usage. The end function moves the internal pointer to, and outputs, the last element in the array. Related methods: current - returns the value of the current element in an array next - moves the internal pointer to, and outputs, the next element in the array prev - moves the internal pointer to, and outputs, the previous element in the array.
- Google allows users to search the Web for images, news, products, video, and other content.
- The for loop statement. The for statement is used when you know how many times you want to.
Any website or progrm error handling is an important part.
Error handling is the technque of catching errors occured by the program and so some action.
Php Exit If Statement
Types of error handling methods:
- Simple die() function
- Custom error handling function
PHP die() function
Php Exit Page
PHP die() function : Using die() function, When error occur in program the program will stop further execution of code and generate proper error message on user screen.
The die() function display a message and stop the further program execution.
The die() function is a simple and basic error handling mechanism in PHP.
PHP die() function syntax
PHP die() Function Example
In below example we use fopen() function to open text file in write mode, But the text file does not exists at specified location, then the code generate system error message.
$myfile = fopen(“meera.txt”,”w”);
Above code generate system warning message:
No file or directory found.
To handle this types of error message we must check if file exists or not before use it.
We can use here die() function to prevent above error.
In above code if the “meera.txt” file does nto exists on location then die() function print error message like ” File Not Found !!” , and exit the current program.
PHP Custom Error Handling Function
In custom error handling we create own function to handling any errors in php. When any error occurs in program the function will be called.
This function can have maximum up to five parameter and minimum two parameters.
Error Function Syntax:
In above syntax we must write two parameter : error_level and error_message other are optional.
Custom Error Handling Parameter
- error_level : Specifies the error report level for the user-defined error. Must be a value number.
- error_message : Specifies the error message for the user-defined error
- error_file : Specifies the filename in error occurred.
- error_line : Specifies the line number where error occurred.
- error_context : Specifies an array containing every variable and their values in use when error occurred.
Error Report Level
The report levels are the different types of error the user-defined error handler can be used for.
|1||.E_ERROR : Fatal run-time errors. Execution of the script is halted|
|2||E_WARNONG : Non-fatal run-time errors. Execution of he script is not halted.|
|4||E_PARSE : Compile-time parse errors. Parse errors should only be generated by the parser.|
|8||E_NOTICE : Run-time notices. The script found something that might be an error, but could also happen when running a script normally.|
|16||E_CORE_ERROR : Fatal errors that occur during PHP’s initial start-up.|
|32||E_CORE_WARNING : Non-fatal run-time errors. This occurs during PHP’s initial start-up.|
|256||E_USER_ERROR : Fatal user-generated error. This is like an E_ERROR set by the programmer using the PHP function trigger_error().|
|512||E_USER_WARNING : Non-fatal user-generated warning. This is like an E_WARNING set by the programmer using the PHP function trigger_error().|
|1024||E_USER_NOTICE : User-generated notice. This is like an E_NOTICE set by the programmer using the PHP function trigger_error().|
|4096||E_RECOVERABLE_ERROR : Catchable fatal error. This is like an E_ERROR but can be caught by a user defined handle (see also set_error_handler()).|
|8191||E_ALL : All errors and warnings, except level E_STRICT (E_STRICT will be part of E_ALL as of PHP 6.0).|
The default error handler is the built in error handler in php. but we try to handle some of errors using custom error handler then we need to set it using PHP built-in library set_error_handler function.