Error handling in PHP refers to the process of managing errors that occur during the execution of a PHP script. It involves detecting, reporting, and resolving various kinds of errors such as syntax errors, runtime errors, and semantic errors. Effective error handling ensures that a script can handle unexpected situations in a graceful manner without crashing.
In PHP, error handling is performed using various functions and constructs like `error_reporting()`, `set_error_handler()`, `trigger_error()`, and the try-catch blocks introduced in PHP 5 with Exception handling.
1. Basic Error Handling: – error\_reporting(): This function sets the error reporting level, allowing you to specify which errors should be displayed or logged. Example: \`\`\`php error_reporting(E_ALL); ini_set(‘display_errors’, 1); \`\`\` This code will report all types of errors and display them on the screen.
– set_error_handler(): A custom error handler can be set using this function. It allows you to define how different types of errors should be handled. Example: \`\`\`php function customError($errno, $errstr, $errfile, $errline) { echo “Error [$errno]: $errstr in $errfile on line $errline\n”; } set_error_handler(“customError”); \`\`\` The above code sets `customError` function to handle errors. – trigger\_error(): This function is used to generate a user-level error/warning/notice message. Example: \`\`\`php if ($value > 10) { trigger_error(“Value must be 10 or less”, E_USER\_WARNING); } \`\`\`1. Exception Handling: Exception handling in PHP allows for catching and managing exceptions in a manner similar to other programming languages like Java and C#. The `try`, `catch`, and `finally` blocks are used to handle exceptions.
Example: \`\`\`php try { throw new Exception(“An error occurred”); } catch (Exception $e) { echo “Caught exception: “ . $e->getMessage(); } finally { echo “This block always executes.”; } \`\`\`1. Custom Errors: Custom errors go a step further than basic error handling by allowing developers to create their own error types and handling mechanisms that can be tailored to the application’s needs.
– Custom Exceptions: Developers can extend the built-in `Exception` class to create custom exception types. Example: \`\`\`php class CustomException extends Exception { public function errorMessage() { return “Error on line “. $this->getLine() .” in “. $this->getFile() .”: ”. $this->getMessage() .” is not a valid E-Mail address”; } } $email = “someone@example..com”; try { if(filter_var($email, FILTER_VALIDATE\_EMAIL) === FALSE) { throw new CustomException($email); } } catch (CustomException $e) { echo $e->errorMessage(); } \`\`\` – Custom Error Handling Classes: You can also create complex error handling mechanisms by defining classes to handle different types of errors and log them or display them accordingly. Example: \`\`\`php class ErrorHandler { public static function handle($errno, $errstr, $errfile, $errline) { // Log the error error\_log(“Error [$errno]: $errstr in $errfile on line $errline”, 3, “error.log”); // Display a user-friendly message echo “An error occurred! Please try again later.”; } } set_error_handler(array(‘ErrorHandler’, ‘handle’)); \`\`\`Sources:
1. [PHP Manual – Error Handling](https://www.php.net/manual/en/book.errorfunc.php)
2. [PHP.net – Exception Handling](https://www.php.net/manual/en/language.exceptions.php)
3. [W3Schools – PHP Error Handling](https://www.w3schools.com/php/php_error.asp)
4. [GeeksforGeeks – PHP Error handling](https://www.geeksforgeeks.org/php-error-handling/)