Open In App

PHP | Types of Errors

Last Updated : 09 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In PHP, errors are an essential part of the development process. They help developers identify issues with their code and ensure that applications run smoothly. PHP provides several types of errors. Understanding these error types is important for troubleshooting and debugging effectively.

In this article will cover the various types of errors in PHP, how they differ, and how you can handle them for more efficient development.

Types of Errors in PHP

In PHP there are the four types of the errors:

  • Syntax Error or Parse Error
  • Fatal Error
  • Warning Error
  • Notice Error

1. Syntax Error(Parse error)

A syntax error, also known as a parse error, occurs when there is a mistake in the PHP code’s syntax. PHP’s parser cannot understand the code due to incorrect usage of language rules, such as missing semicolons, parentheses, or braces.

Example:

PHP
<?php
echo Hello World  
?>



Output
Parse error: syntax error, unexpected 'World' (T_STRING), expecting ';' or ',' in /home/guest/sandbox/Solution.php on line 2

2. Fatal Error

A fatal error in PHP is a serious problem that causes the script to stop executing immediately. Fatal errors occur when PHP cannot proceed with the execution of a script due to critical issues, such as calling an undefined function or class.

Example:

PHP
<?php
undefinedFunction();  // Function is not defined
?>

Output
Fatal error: Uncaught Error: Call to undefined function undefinedFunction() in /home/guest/sandbox/Solution.php:2
Stack trace:
#0 {main}
  thrown in /home/guest/sandbox/Solution.php on line 2

3. Warning Errors

Warnings are non-fatal errors. PHP will continue executing the script even after a warning occurs. They are typically issued when PHP encounters issues that do not prevent script execution but might lead to unexpected behavior

Example:

PHP
<?php
include("nonexistentfile.php");  // File does not exist
?>

Output
Warning: include(nonexistentfile.php): failed to open stream: No such file or directory in /home/guest/sandbox/Solution.php on line 2

Warning: include(): Failed opening 'nonexistentfile.php' for inc...

4. Notice Error

Notice errors are the least severe type of PHP errors. These are typically used to notify developers of minor issues in the code, such as accessing an undefined variable. Notice errors do not interrupt script execution and often go unnoticed unless explicitly logged.

Example:

PHP
<?php
echo $undefinedVariable;  // Accessing an undefined variable
?>

Output:

Undefined variable $undefinedVariable in /tmp/tdcD60JcaH/main.php on line 2

PHP Error Constants and Their Descriptions

  • E_ERROR : A fatal error that causes script termination
  • E_WARNING : Run-time warning that does not cause script termination
  • E_PARSE : Compile time parse error.
  • E_NOTICE : Run time notice caused due to error in code
  • E_CORE_ERROR : Fatal errors that occur during PHP’s initial startup (installation)
  • E_CORE_WARNING : Warnings that occur during PHP’s initial startup
  • E_COMPILE_ERROR : Fatal compile-time errors indication problem with script.
  • E_USER_ERROR : User-generated error message.
  • E_USER_WARNING : User-generated warning message.
  • E_USER_NOTICE : User-generated notice message.
  • E_STRICT : Run-time notices.
  • E_RECOVERABLE_ERROR : Catchable fatal error indicating a dangerous error
  • E_DEPRECATED : Run-time notices.

Conclusion

PHP provides several types of errors, each designed to address different levels of issues in your code. From simple syntax mistakes to more complex runtime errors, understanding these error types helps you troubleshoot and fix problems more effectively.



Next Article
Article Tags :

Similar Reads