"I am Saqib Jahangir. A passionate vlogger, software engineer, trainer and avid traveler with a deep love for exploring the hidden gems of our beautiful planet. With a strong foundation in Application Development, Application Architecture & Database Design and Product Management, I bring over a decade of hands-on experience building secure, scalable, and resilient web applications for a diverse range of industries."

Error Handling in PHP

 

When writing PHP applications, errors are bound to happen — maybe a missing file, wrong database credentials, or a typo in your code. Error handling is the process of catching these errors and responding gracefully, instead of letting the program crash or show confusing messages to the user.


Types of Errors in PHP

PHP categorizes errors into a few main types:

  1. Parse Errors (Syntax Errors):
    • Caused by mistakes in code syntax.
    • Example: Missing a semicolon (;).

2.  <?php

3.  echo "Hello World" // Missing semicolon

4.  ?>

  1. Fatal Errors:
    • Happen when PHP encounters something it can’t execute.
    • Example: Calling an undefined function.

6.  <?php

7.  testFunction(); // Function not defined

8.  ?>

  1. Warning Errors:
    • Not critical, script continues to run.
    • Example: Including a file that doesn’t exist.

10.<?php

11.include("file.php"); // File not found, but script continues

12.?>

  1. Notice Errors:
    • Minor errors, often due to uninitialized variables.
    • Example:

14.<?php

15.echo $username; // Undefined variable

16.?>


Error Handling Techniques in PHP

1. Using error_reporting()

You can control which errors PHP should report.

<?php

// Report all errors

error_reporting(E_ALL);

 

// Report only warnings and errors

error_reporting(E_ERROR | E_WARNING);

?>

2. Using try...catch (Exceptions)

Exceptions let you handle errors gracefully without breaking the application.

<?php

try {

    if (!file_exists("data.txt")) {

        throw new Exception("File not found!");

    }

    $file = fopen("data.txt", "r");

} catch (Exception $e) {

    echo "Error: " . $e->getMessage();

}

?>

3. Custom Error Handler

You can define your own error handling function using set_error_handler().

<?php

function customError($errno, $errstr) {

    echo "Error [$errno]: $errstr<br>";

}

set_error_handler("customError");

 

// Trigger an error

echo $test; // Undefined variable

?>

4. Logging Errors

Instead of showing errors to users, you can log them into a file:

<?php

ini_set("log_errors", 1);

ini_set("error_log", "errors.log");

 

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

?>


🎯 Best Practices for Error Handling

  • Don’t show detailed errors to users in production (use logs instead).
  • Use try...catch for database queries, file handling, or risky operations.
  • Log critical errors for debugging.
  • Use custom error pages for a better user experience.

👉"Error handling in PHP ensures that your applications run smoothly and don’t break unexpectedly. By using exceptions, custom handlers, and proper logging, developers can create robust and user-friendly applications."

 

Popular Posts

Operators (Arithmetic, Comparison, Logical)

Functions (Built-in & User-defined)