Types of Errors in PHP
The blog will help you learn about the many types of PHP errors. But, before we get into the specifics and details of the various errors, it’s important to understand what an error in PHP is. An error in PHP may be described as the incorrect execution of a PHP script or the unexpected end of a PHP code while it is being executed. An error in PHP can occur due to many reasons, including improper syntax, poor coding, or utilizing an outdated PHP version.
Whether you’re a novice or an experienced individual, the potential of making a mistake is always there. A competent PHP programmer, on the other hand, may make fewer mistakes than a newbie when it comes to developing PHP scripts quickly. In the following section, we will mention all types of errors in PHP along with proper explanations and examples.
Types of Errors in PHP
In simple terms, an error is a mistake in a PHP program that can be produced by improper syntax or codes. Your code compiler will display an error notice that includes the filename along with its location, a message detailing the error, and the line number where the error occurred.
In PHP, there are primarily four types of errors to consider:
- Syntax Error or Parse Error
- Fatal Error
- Warning Error
- Notice Error
Let’s take a closer look at each one.
1. Syntax Error or Parse Error
A syntax error is a mistake in source code syntax that programmers might make owing to a lack of attention or expertise. Parse error is another name for it. During the time of compilation, the compiler is utilized to catch the syntax problem.
Unclosed quotes, misplaced semicolons, excess or missing parentheses, or even unclosed brackets are some of the common reasons that result in these errors. The compiler may detect syntax problems when compiling the application. It displays a syntactic or parsing error message.
Example: Missing semicolons
echo “Hy! What’s your name? </br>”;
echo “Hi, I am Bob. How are you?”
echo “I’m good. Hope you’re having a good day.”;
echo “Yes! Weekend fun. Wishing you the same”;
Output: Parse error: syntax error, unexpected ‘echo’ (T_ECHO), expecting ‘,’ or ‘;’ in C:\xampp\htdocs\program\fatalerror.php on line 5
Explanation: A semicolon (;) was omitted in line 5 of the preceding example. As a result, a parsing error was triggered, and an error message was shown in the browser, as seen in the output.
2. Fatal Error
Another form of error that occurs when an undefined function is used is a fatal error. The PHP compiler detects the undefined function as well as the PHP code. The PHP compiler raises a fatal error when a function is invoked without giving its definition.
Following are the three different types of fatal errors:
- Startup fatal error: When the machine fails to run the code during installation.
- Compile time fatal error: Occurs when an individual tries to use nonexistent data.
- Runtime fatal error: Occurs when the program is running, which eventually causes the code to stop working completely.
Example: Undefined function
function add($x, $y)
$sum = $x + $y;
echo “sum = ” . $sum;
$x = 0;
$y = 20;
PHP Fatal error: Uncaught Error:
Call to undefined function diff()
thrown in /home/36db1ad4634ff7deb7f7347a4ac14d3a.php on line 12
Explanation: The function is called on line 12, however, the definition of the function is not provided. As a result, it returns an error.
3. Warning Error
When the developer tries to incorporate a file that does not exist in the PHP script, a warning error is generated. When a programmer doesn’t utilize the right number of arguments in a function, several problems might arise. It does not affect the execution of a PHP script, much like notice errors. However, the error will be displayed on the screen. Have a look at some of the most common causes of warning error:
- Using an external file that doesn’t exist in the current directory.
- Using parameters in a function that are incorrect.
Example: Include missing file
$cmpny = ‘javatpoint’;
echo “Warning Error: “;
//include a file in the code
Warning: include(jtp.php): failed to open stream: No such file or directory in C:\xampp\htdocs\program\fatalerror.php on line 7
Warning: include(): Failed opening ‘jtp.php’ for inclusion (include_path=’C:\xampp\php\PEAR’) in C:\xampp\htdocs\program\fatalerror.php on line 7
Explanation: In this case, we attempted to include a file that doesn’t exist in our application. As a result, a warning and an error message were displayed on the screen.
4. Notice Error
Notice error is quite similar to warning error. It indeed appears when the program has something wrong, but it continues the program’s execution notwithstanding the notification error. Therefore, the execution of the code is not hampered by the notice error.
Example: Access to an undefined variable
A notice error appears when we attempt to utilize or access an undefined variable.
$x = “XYZcompany”;
PHP Notice: Undefined variable: geeks in
/home/84c47fe936e1068b69fb834508d59689.php on line 5
Explanation: As a result of the undeclared variable $xyz, this application generates an error message.
PHP Error Levels
|E_ERROR||A fatal error that results in the script being terminated.|
|E_WARNING||It is a run-time warning that doesn’t terminate scripts.|
|E_PARSE||Parse error appears during the time of code compile.|
|E_NOTICE||Due to a code error, this run time notification appears.|
|E_CORE_ERROR||Fatal errors that occur during PHP’s first launch.|
|E_CORE_WARNING||Warnings that appear during the initialization of PHP.|
|E_COMPILE_ERROR||Fatal compile-time errors indicate scripting issues.|
|E_USER_ERROR||Error message created by the user.|
|E_USER_WARNING||Warning message created by the user.|
|E_USER_NOTICE||Notice message created by the user.|
|E_STRICT||Notifications during the runtime.|
|E_RECOVERABLE_ERROR||A fatal mistake can be caught and indicates a potentially dangerous situation.|
|E_DEPRECATED||Notifications appearing during the runtime.|
Understanding the four types of PHP errors will assist you in rapidly identifying and resolving them in the scripts. Therefore, it’s important to pay attention to output messages, since they typically contain information about other issues or cautions. It’s also vital to know which PHP version your web server is running if you’re attempting to track down an issue for your codes.