important PHP Error Handling Interview Questions

🎯 PHP Error Handling – Interview Questions

🟢 Basic Level

1. What is error handling in PHP?

Answer:
Error handling is the process of detecting, managing, and responding to runtime or syntax errors in a PHP script.


2. What are the types of errors in PHP?

Answer:

  • Notice
  • Warning
  • Fatal Error
  • Parse Error
  • Deprecated Error

3. Difference between Warning and Fatal Error?

WarningFatal Error
Script continuesScript stops
Non-criticalCritical issue

4. What is error_reporting()?

Answer:
It defines which errors should be reported.

error_reporting(E_ALL);

5. How to display errors in PHP?

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

🟡 Intermediate Level

6. What is Exception Handling?

Answer:
A mechanism to handle runtime errors using try, catch, and throw.


7. Explain try-catch block.

try {
// risky code
} catch (Exception $e) {
echo $e->getMessage();
}

8. What is the use of throw?

Answer:
It is used to generate an exception manually.


9. Difference between Error and Exception?

ErrorException
System-level issueApplication-level issue
Cannot always be handledCan be handled using try-catch

10. What is Throwable in PHP?

Answer:
Base interface for both Error and Exception (PHP 7+).


🔵 Advanced Level

11. What is a custom error handler?

Answer:
A user-defined function to handle errors using set_error_handler().

set_error_handler("myErrorFunction");

12. What is trigger_error()?

Answer:
Used to create a user-defined error.

trigger_error("Custom error", E_USER_WARNING);

13. How to log errors in PHP?

error_log("Error message", 3, "error.log");

14. What is the difference between die() and exception?

die()Exception
Stops script immediatelyCan be handled
No recoveryRecovery possible

15. How to handle database errors in PHP?

Answer:
Using try-catch with PDO or checking connection errors.


16. What is PDO exception mode?

$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

17. What are best practices for error handling?

Answer:

  • Hide errors in production
  • Log errors instead
  • Use try-catch blocks
  • Validate inputs
  • Use custom handlers

🔥 Scenario-Based Questions

18. What will happen in this code?

echo 10/0;

Answer:
Division by zero → Warning or Error (PHP version dependent)


19. How will you handle API failure?

Answer:

  • Use try-catch
  • Check response status
  • Log errors
  • Show user-friendly message

20. How do you debug a white screen error?

Answer:

  • Enable error reporting
  • Check logs
  • Use var_dump()
  • Check syntax errors

🚀 Pro Interview Questions

21. Can we catch fatal errors in PHP?

Answer:
Not directly, but can use:

  • register_shutdown_function()

22. What is shutdown function?

register_shutdown_function(function() {
print_r(error_get_last());
});

23. How to convert errors into exceptions?

set_error_handler(function($errno, $errstr) {
throw new ErrorException($errstr, 0, $errno);
});

24. What is stack trace?

Answer:
A report of function calls leading to an error.


25. How to handle errors in production?

Answer:

  • Disable display_errors
  • Enable logging
  • Monitor logs
  • Use centralized error tracking

🎯 Bonus Tip (Interview Hack)

If interviewer asks:
👉 “How do you handle errors in real projects?”

Say:

“I use try-catch for critical operations, enable logging instead of displaying errors in production, use custom error handlers, and monitor logs for debugging.”