0% found this document useful (0 votes)
38 views

PHP Errors

The document discusses three common PHP error handling methods: die statements, custom error handlers, and PHP error reporting. Die statements immediately terminate script execution and output an error message. Custom error handlers are user-defined functions called when errors occur. PHP error reporting outputs built-in error messages depending on settings.

Uploaded by

Sinley Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

PHP Errors

The document discusses three common PHP error handling methods: die statements, custom error handlers, and PHP error reporting. Die statements immediately terminate script execution and output an error message. Custom error handlers are user-defined functions called when errors occur. PHP error reporting outputs built-in error messages depending on settings.

Uploaded by

Sinley Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Error and Exception Handling

We are going to look at three (3) commonly used methods;


Die statements– the die function combines the echo and exit function in one. It is very
useful when we want to output a message and stop the script execution when an error occurs.
Custom error handlers – these are user defined functions that are called whenever an error
occurs.
PHP error reporting – the error message depending on your PHP error reporting settings.
This method is very useful in development environment when you have no idea what caused
the error. The information displayed can help you debug your application.

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");
}

// Otherwise, return the result of the division


return $numerator / $denominator;
}
// Call the divide function with different parameters
echo divide(10, 2) . "<br>"; // Output: 5
echo divide(6, 0) . "<br>"; // Output: Error: Cannot divide by zero (script terminated)
echo divide(8, 4) . "<br>"; // This line of code will not be executed due to the previous
script termination
?>

Custom error handlers


<?php
$denominator = 0;
echo 2 / $denominator;
?>

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 );

function handleError($errno, $errstr,$error_file,$error_line) {


echo "<b>Error:</b> [$errno] $errstr - $error_file:$error_line";
echo "<br />";
echo "Terminating PHP Script";

die();
}

//set error handler


set_error_handler("handleError");

//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.

You might also like