Last updated on September 21, 2023
We make mistakes during software development by using different programming languages. An error is a type of mistake. PHP is a server-side scripting language, which is mostly used for web development. A PHP Error occurs when something is wrong in the PHP code. Understanding these errors is very important for PHP developers. An error is a type of mistake. There are several types of Errors in PHP. let’s try to understand in detail about errors in PHP.
Types of Errors in PHP:
In PHP, we experience four types of errors which are given below:
01. Parse Error or Syntax Error:
Parse errors can occur due to issues such as unclosed quotes, missing or extra parentheses, unclosed braces, or missing semicolons, among others.
In the below example, we have a script that has a missing semicolon at the end of the Tokyo city string.
echo "New York City";
echo "Tokyo"
echo "Paris";
// output : ERROR! Parse error: syntax error, unexpected token "echo", expecting "," or ";" in cities.php on line 4
We missed the semicolon in the second line after the string that contains “Tokyo” in the code. When these types of mistakes happen, there will be a parse or syntax error.
02. Fatal Errors:
It is the type of error where PHP understands the code but recognizes an undefined function. This error occurs when we try to call a function that has not been defined.
Fatal errors stop the execution of the script. If you are trying to access the undefined functions, then the output is a fatal error.
function studentName1()
{
return "John Smith";
}
studentName2();
// output : ERROR! Fatal error: Uncaught Error: Call to undefined function fun2() in animalName.php:6 Stack trace: #0 {main} thrown in animalName.php on line 6
In this example, we defined a function `studentName1()` that returns that student’s name. But we are calling it by the incorrect name `studentName2()`
. Since studentName2() is not defined in the code that’s why PHP returns a fatal error.
03. Warning Errors:
Warning errors will not stop the script. The main reason for warning errors is to include a missing file or use the incorrect number of parameters in a function.
echo "Learn programming with CoderAdvise";
include ("product_listing.php");
// output : Warning Error!!Warning: include(product_listing.php): Failed to open stream: No such file or directory in xyz.php on line 3.
In the above example of code, we are trying to include a file whose name is product_listing into xyz.php. However, the product_listing.php file does not exist in the directory. That’s why PHP is returning a warning error.
04. Notice Errors:
The Notice Error is almost the same as a Warning Error. Notice error comes we called an undefined variable. When we call an undefined variable we will see a notice errors in PHP.
$a = "John Smith";
echo "Notice Error !!";
echo $b;
// output : Warning: Undefined variable $b in personName.php on line 4
In this example, we just defined $a, but we are calling $b, which is not defined. That’s why Notice Errors is produced.
Conclusion:
In this article, we define the PHP error types in detail along with examples. Also, this article explains when these errors come up in our web application.