PHP Errors
PHP Errors
In PHP, die() is a language construct that is used to immediately terminate the execution of
the script and display an error message to the user.
When the die() function is called, the script execution stops at that point, and no further
code is executed. The error message that is displayed to the user is whatever string is
passed as an argument to the die() function. For example, die("Something went
wrong!"); will terminate the script and display the message "Something went wrong!" to the
user.
The die() function is often used in error handling, to gracefully handle unexpected errors
that occur during the execution of the script. By terminating the script and displaying an
error message, users can be informed of the problem, and developers can be notified of the
issue so that it can be addressed.
Note that die() and its alias exit() are equivalent, and can be used interchangeably in PHP.
<?php
if(!file_exists("/tmp/test.txt")) {
die("File not found");
}else {
$file = fopen("/tmp/test.txt","r");
print "Opend file sucessfully";
}
// Test of the code here.
?>
Example:
<?php
// Define a function that divides two numbers
function divide($numerator, $denominator) {
// Check if the denominator is zero
if ($denominator == 0) {
// If so, terminate the script with an error message
die("Error: Cannot divide by zero");
}
As you can see from the above results, it makes our application look unprofessional and can
be annoying to the user.
We will modify the above code and write an error handler for the application
<?php
$denominator = 0;
if ($denominator != 0) {
echo 2 / $denominator;
} else {
echo "cannot divide by zero (0)";
}
?>
Defining Custom Error Handling Function
you can write your own function to handling any error. PHP provides you a
framework to define error handling function.
This function must be able to handle a minimum of two parameters (error level
and error message) but can accept up to five parameters (optionally: file, line-
number, and the error context)
Syntax
error_function(error_level,error_message, error_file,error_line,error_context);
<?php
function my_error_handler($error_no, $error_msg)
{
echo "Opps, something went wrong:";
echo "Error number: [$error_no]";
echo "Error Description: [$error_msg]";
}
set_error_handler("my_error_handler");
echo (5 / 0);
?>
As you can see from the above example, custom error handlers are powerful in the sense that
They allow us to customize the error messages.
The custom error handler can also include error logging in a file/database,
emailing the developer etc.
Possible Error levels
These error report levels are the different types of error the user-defined error handler
can be used for.
PHP error reporting
basic syntax
<?php
error_reporting($reporting_level);
?>
“error_reporting” is the PHP error reporting function
“$reporting_level” is optional, can be used to set the reporting level. If no reporting
level has been specified, PHP will use the default error reporting level as specified in
the php.ini file.
Once you define your custom error handler you need to set it using PHP built-in library
set_error_handler function. Now lets examine our example by calling a function which
does not exist.
<?php
error_reporting( E_ERROR );
die();
}
//trigger error
myFunction();
?>
O/P
Fatal error: Call to undefined function myFunction() - /path/to/your/script.php:17
Terminating PHP Script
the myFunction() function is not defined in the code, and when it is called, it will trigger a
fatal error. Since a custom error handler function handleError() has been defined and
registered using the set_error_handler() function, this function will be called to handle the
error.