BCA / B.Tech 13 min read

Error Handling in PHP | What is Error Handling in PHP?

Error Handling in PHP in Hindi |  What is Error Handling in PHP?


  • Through Error Reporting and Error Handling in PHP, we can not only understand errors but also control their impact. 
  • Adopting the right methods to identify and handle different types of errors makes the coding process smoother and more secure.
Detailed information about Error Reporting, Error Handling, and Error Types in PHP is given below.

Error Reporting in PHP: 

  • Error Reporting means showing or hiding errors that occur in a PHP script. This helps in understanding problems in the code during development.
Methods of Error Reporting in PHP:

ini_set() function: This sets the error reporting level at runtime.

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

php.ini file: The php.ini file is used to set error reporting at the server level.

display_errors = On
display_startup_errors = On
error_reporting = E_ALL

error_reporting() function: It is used to dynamically control error reporting in the code.

error_reporting(E_ALL); // Show all errors
error_reporting(0);     // Show no errors
?>

Common Error Reporting Constants:

  • E_ALL: Show all errors and warnings.
  • E_ERROR: Show only fatal errors.
  • E_WARNING: Show warning errors but the script will not stop.
  • E_NOTICE: Show runtime notices.
  • E_DEPRECATED: Show warnings related to deprecated features.

Error Handling in PHP

Error Handling means controlling the behavior of the script when an error occurs. There are different ways to handle errors in PHP.

Methods of Error Handling in PHP:

try-catch block: This is used for Exception Handling.

try {
    // Code that may cause an error
    if (true) {
        throw new Exception("This is an error!");
    }
} catch (Exception $e) {
    echo "Error: " . $e->getMessage();
}
?>

set_error_handler() function: This is used to implement custom error handling.

function customError($errno, $errstr) {
    echo "Error [$errno]: $errstr
";
}
set_error_handler("customError");
echo $undefined_var; // Notice error
?>

error_log() function: This is used to log errors to a file.

error_log("This is an error message.", 3, "errors.log");
?>

register_shutdown_function(): This function is used to handle any final error when the script closes.

function shutdownHandler() {
    $error = error_get_last();
    if ($error) {
        echo "Error during shutdown: {$error['message']}";
    }
}
register_shutdown_function("shutdownHandler");
?>

 Types of Errors in PHP:

Errors in PHP are mainly of four types:

Parse Errors: These are caused by syntax errors. Example:

echo "Hello World" // Missing semicolon
?>

Fatal Errors: This occurs when a required resource is not found. The script stops there.

include("non_existing_file.php");
?>

Warning Errors: Warning errors only show a message but the script does not stop.

echo $undefined_variable; // Undefined variable warning
?>

Notice Errors: These are for minor problems, such as using an unassigned variable.

echo $unassigned_variable;
?>

Error Handling Best Practices:

  • Show all errors in development: error_reporting(E_ALL);
  • Hide and log errors in production.
  • Use custom Error Handlers.
  • Handle Exceptions correctly.
  • Set up different logging systems for errors.