PHP | Types of Errors (original) (raw)

Last Updated : 09 Apr, 2025

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:

**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 `

`

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 `

`

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 `

`

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 `

`

**Output:

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

**PHP Error Constants and Their Descriptions

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.