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
?>
OutputParse 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
?>
OutputFatal 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
?>
OutputWarning: 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.
Similar Reads
PHP trigger_error() Function
The trigger_error() function is an inbuilt function in PHP that has the capacity to act as a built-in error handler. This function is generally used by programmers to trigger or generate user level errors, notices or messages. It returns a Boolean value i.e. it returns TRUE on success and FALSE on f
2 min read
PHP Data Types
In PHP, data types define the kind of value a variable can hold. PHP is a loosely typed language, meaning you donât need to declare the data type of a variable. It is automatically assigned based on the value. But it is important to understand data types because it is important for writing reliable,
4 min read
PHP Operators
In PHP, operators are special symbols used to perform operations on variables and values. Operators help you perform a variety of tasks, such as mathematical calculations, string manipulations, logical comparisons, and more. Understanding operators is essential for writing effective and efficient PH
9 min read
PHP 8 Union types
A âunion typeâ accepts values of multiple different data types, rather than a single one. If the programming language supports union types, you can declare a variable in multiple types. For example, there can be a function that can accept the variable of type âstringâ or âfloatâ as a parameter. PHP
2 min read
PHP | xml_error_string() Function
Pre-requisite: XML BasicsThe xml_error_string() function is an inbuilt function in PHP which returns the XML parser error description for generated error code. Syntax: string xml_error_string( int $error_code) Parameters: This function accepts single parameter $error_code which is required. It speci
3 min read
How do I get PHP errors to display?
There are four ways to display errors in PHP which are listed below: error_reporting: It does not display the E-STRICT, E-NOTICE and E_DEPRECATED level errors and display all the other level errors.display_errors: Its default value is "off". Set it to "on".log_errors: It's default value is "on" whic
2 min read
PHP restore_error_handler() Function
The restore_error_handler() function is an inbuilt PHP function that facilitates restoring the previous version of an error handler function. Syntax: restore_error_handler(): boolParameter: This function does not accept any parameter. Return Value: This function will always return "true". Example 1:
1 min read
PHP | settype() Function
The settype() function is a built-in function in PHP. The settype() function is used to the set the type of a variable. It is used to set type or modify type of an existing variable. Syntax: boolean settype($variable_name, $type) Parameters: The settype() function accepts two parameters as shown in
2 min read
PHP | mysqli_error() Function
The mysqli_error() function is used to return the error in the most recent MySQL function call that failed. If there are multiple MySQL function calls, the error in the last statement is the one that is pointed out by the function. Syntax: mysqli_error("database_name") Parameters: This function acce
1 min read
PHPUnit: Testing Framework for PHP
PHPUnit is a programmer-oriented testing framework for PHP. It is an instance of the xUnit architecture for unit testing frameworks. It is used for the purpose of unit testing for PHP code. PHPUnit was created by Sebastian Bergmann and its development is hosted on GitHub. Purpose of the PHPUnit Fram
2 min read