Introduction
PHP is a powerful scripting language used to create dynamic webpages. However, like any other programming language, it is prone to errors. There are four main types of errors in PHP: syntax errors, runtime errors, logic errors, and fatal errors. Syntax errors occur when the code does not follow the correct syntax of the language. Runtime errors occur when the code is syntactically correct but fails to execute properly. Logic errors occur when the code produces an unexpected result due to incorrect logic. Finally, fatal errors occur when the code cannot be executed due to a fatal error. In this article, we will discuss each of these errors in detail and how to fix them.
4 Different Types of Errors in PHP
1. Syntax Errors: These are errors that occur when the syntax of a code is incorrect.
2. Logical Errors: These are errors that occur when the logic of a code is incorrect.
3. Runtime Errors: These are errors that occur when a code is executed and an unexpected result occurs.
4. Compilation Errors: These are errors that occur when a code is compiled and an unexpected result occurs.
A PHP Error occurs when something is wrong in the PHP code. The error can be as simple as a missing semicolon, or as complex as calling an incorrect variable.
To efficiently resolve a PHP issue in a script, you must understand what kind of problem is occurring.
The four types of PHP errors are:
1. Warning Error
2. Notice Error
3. Parse Error
4. Fatal Error
Tip: You can test your PHP scripts online. We used an online service to test the code mentioned in this article.
Warning Error
A warning error in PHP does not stop the script from running. It only warns you that there is a problem, one that is likely to cause bigger issues in the future.
The most common causes of warning errors are:
- Calling on an external file that does not exist in the directory
- Wrong parameters in a function
For instance:
<?php
echo "Warning error"';
include ("external_file.php");
?>
As there is no “external_file”, the output displays a message, notifying it failed to include it. Still, it doesn’t stop executing the script.
Notice Error
Notice errors are minor errors. They are similar to warning errors, as they also don’t stop code execution. Often, the system is uncertain whether it’s an actual error or regular code. Notice errors usually occur if the script needs access to an undefined variable.
Example:
<?php
$a="Defined error";
echo "Notice error";
echo $b;
?>
In the script above, we defined a variable ($a), but called on an undefined variable ($b). PHP executes the script but with a notice error message telling you the variable is not defined.
Parse Error (Syntax)
Parse errors are caused by misused or missing symbols in a syntax. The compiler catches the error and terminates the script.
Parse errors are caused by:
- Unclosed brackets or quotes
- Missing or extra semicolons or parentheses
- Misspellings
For example, the following script would stop execution and signal a parse error:
<?php
echo "Red";
echo "Blue";
echo "Green"
?>
It is unable to execute because of the missing semicolon in the third line.
Fatal Error
Fatal errors are ones that crash your program and are classified as critical errors. An undefined function or class in the script is the main reason for this type of error.
There are three (3) types of fatal errors:
- Startup fatal error (when the system can’t run the code at installation)
- Compile time fatal error (when a programmer tries to use nonexistent data)
- Runtime fatal error (happens while the program is running, causing the code to stop working completely)
For instance, the following script would result in a fatal error:
<?php
function sub()
{
$sub=6-1;
echo "The sub= ".$sub;
}
div();
?>
The output tells you why it is unable to compile, as in the image below:
Conclusion
Distinguishing between the four types of PHP errors can help you quickly identify and solve problems in your script. Make sure to pay attention to output messages, as they often report on additional issues or warnings. If you are trying to locate a bug on your website, it is also important to know which PHP version your web server is running.
4 Different Types of Errors in PHP
PHP is a powerful scripting language used to create dynamic web pages. It is also prone to errors, which can be difficult to debug. Here are four common types of errors in PHP:
Syntax Errors
Syntax errors occur when the code does not follow the correct syntax of the language. This can be caused by missing semicolons, incorrect variable names, or incorrect use of quotation marks. Syntax errors can be difficult to debug, as they can be caused by a variety of factors.
Runtime Errors
Runtime errors occur when the code is syntactically correct, but fails to execute properly. This can be caused by incorrect data types, incorrect function calls, or incorrect logic. Runtime errors can be difficult to debug, as they can be caused by a variety of factors.
Logic Errors
Logic errors occur when the code is syntactically correct and executes properly, but produces an incorrect result. This can be caused by incorrect logic, incorrect data types, or incorrect function calls. Logic errors can be difficult to debug, as they can be caused by a variety of factors.
Fatal Errors
Fatal errors occur when the code is syntactically correct and executes properly, but causes the program to crash. This can be caused by incorrect data types, incorrect function calls, or incorrect logic. Fatal errors can be difficult to debug, as they can be caused by a variety of factors.