What are errors in php explain the types of errors with an example each?

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.

What are errors in php explain the types of errors with an example each?

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.

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:

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.

What are errors in php explain the types of errors with an example each?

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:

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.

What are errors in php explain the types of errors with an example each?

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:

It is unable to execute because of the missing semicolon in the third line.

What are errors in php explain the types of errors with an example each?

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:

  1. Startup fatal error (when the system can’t run the code at installation)
  2. Compile time fatal error (when a programmer tries to use nonexistent data)
  3. 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:

The output tells you why it is unable to compile, as in the image below:

What are errors in php explain the types of errors with an example each?

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.

Basically, an error is a mistake in a program that may be caused by writing incorrect syntax or incorrect code. An error message is displayed on your browser containing the filename along with location, a message describing the error, and the line number in which error has occurred.

There are usually different types of error. In PHP, mainly four types of errors are considered:

  1. Syntax Error or Parse Error
  2. Fatal Error
  3. Warning Error
  4. Notice Error

We will discuss all these errors in detail with examples:

Syntax Error or Parse Error

A syntax error is a mistake in the syntax of source code, which can be done by programmers due to their lack of concern or knowledge. It is also known as Parse error. Compiler is used to catch the syntax error at compile time.

Note: Syntax error stop the execution of the code.

These errors can occur due to these common reasons like unclosed quotes, missing semicolon, extra or missing parentheses, or unclosed brackets and many more. While compiling the program, syntax error can be caught by the compiler. It gives a parse error or syntax error message.

Example 1: Missing semicolon

Output

Parse error: syntax error, unexpected 'echo' (T_ECHO), expecting ',' or ';' in C:\xampp\htdocs\program\fatalerror.php on line 5

Explanation: In this above example, a semicolon (;) was missing in line 5. So, it generated a parse error and displayed an error message on browser as given in the output.

Example 2: Missing dollar symbol

Output

Parse error: syntax error, unexpected '=' in C:\xampp\htdocs\program\fatalerror.php on line 5

Explanation: In this above example, dollar ($) symbol was missing in line 5. So, it generated a parse error and displayed an error message on browser as given in the output.

Fatal Error

A fatal error is another type of error, which is occurred due to the use of undefined function. The PHP compiler understands the PHP code but also recognizes the undefined function. This means that when a function is called without providing its definition, the PHP compiler generates a fatal error.

A fatal error is generated when a function is called without its definition. See the below example containing the fatal error -

Example: Calling undefined function

In the above code we have defined the add() function but called other function, which is catch_fatal_error(). Therefore, it generates a fatal error and print an error message on the browser as given below:

Output

Fatal error: Uncaught Error: Call to undefined function catch_fatal_error() in C:\xampp\htdocs\program\fatalerror.php:15 Stack trace: #0 {main} thrown in C:\xampp\htdocs\program\fatalerror.php on line 13

Warning Error

A warning is generated when the programmer tries to include a missing file. The PHP function calls that missing file which does not exist. The warning error does not stop/prevent the execution of the program.

The main reason behind generating a warning error is to pass an incorrect number of parameters to a function or to include a missing file.

Example: Include missing file

Output

Warning Error:
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 example, we tried to include a file in our program, which does not exist. So, it generated a warning and displayed an error message.

Notice Error

Notice error is same as warning error. When program contains something wrong, the notice error occurs. But it allows/continue the execution of the program with a notice error. Notice error does not prevent the execution of the code. For example - access to undefined variable.

Generally, notice error occurs when we try to use or access a variable which is undefined. See the below example to understand it-

Example 2: Access undefined variable

Output

Airtel
Notice: Undefined variable: automobile in C:\xampp\htdocs\program\fatalerror.php on line 6

Explanation: In this above example, we were trying to use a variable $automobile, which was not defined. Therefore, it generated a notice "Undefined variable" and continued the execution of the program.


What is error and types of error in PHP?

An error message is displayed on your browser containing the filename along with location, a message describing the error, and the line number in which error has occurred. There are usually different types of error. In PHP, mainly four types of errors are considered: Syntax Error or Parse Error. Fatal Error.

How many errors are there in PHP?

Basically there are four types of errors in PHP, which are as follows: Parse Error (Syntax Error) Fatal Error. Warning Error.

What is error and error handler in PHP?

Error handling is the process of catching errors raised by your program and then taking appropriate action. If you would handle errors properly then it may lead to many unforeseen consequences. Its very simple in PHP to handle an errors.

What is critical error in PHP?

Fatal errors are critical errors - for example, instantiating an object of a non-existent class, or calling a non-existent function. These errors cause the immediate termination of the script, and PHP's default behavior is to display them to the user when they take place.