Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

php
webdevelopment
bug
communitycreator

What are the types of errors in PHP?

NDUKWE CHIDERA K.

When do bugs occur?

Bugs are caused by mistakes we make when writing codes. As long as it is syntaticErrors caused by improper use of language constructs and not semanticif an error is semantic, it means the code does not make sense when it comes to solving the intended problem, PHP always has a way to let us know about them when we execute. It does this by popping up messages on the screen when we run the code. There are four code error messages.

Types of error in PHP

PHP engine throws messages to the output through the parser to indicate the presence of errors in your code, its description, and which code line they can be found on.

Error types include:

  • Warning error
  • Notice error
  • Parse error
  • Fatal error

1. Warning error

This error is thrown by the PHP parser when things like the following happen in your code:

  • If there is a function that was passed the wrong parameters.

  • If there is a call to another file that could not be found.

These errors won’t stop the parser from executing other correct code lines in the program.

The parser only wishes to point out an error that might escalate to a big challenge later. Hence the warning.

<?php
echo "chill";
//we don't have the file being referred to here
include "many.php";
echo "chill";
//both echo statements will be executed
?>

2. Notice error

This type of error causes the parser to display a simple notice. It is an error that shows that you missed out on a slight requirement such as:

  • Using variables you didn’t define.

These are very similar to warning errors as they only wish to notify you about something they are not sure why you did, although it doesn’t halt the code’s execution.


<?php
$a="Defined error";
echo "Notice error";
echo $b-$a;
?>

The output of the code above is as follows:


3. Parse error

Unlike the notice and warning errors, this error causes the PHP parser to terminate the script execution when encountered.

They are caused by things like:

  • Missing out delimiters like the semicolon ; or using such characters incorrectly.

  • Improperly used or missing quotes, brackets, parentheses, and braces.

  • When functions, variables, or any other name is misspelled.

This kind of error is also known as a syntax error because it is an error that is thrown in cases of wrong syntax usage.

<?php
$a="Syntax is checked first for correctness";
echo just watch it;
echo "Notice error"
$n = 4;
//line 3 and 4 are erronous but the scripts terminates
//on its first encounter of syntax error on line 3
?>

4. Fatal error

As the name implies, fatal errors are like parse errors because they terminate the script if encountered. They are fatal to your code.

These are those errors that can cause your program script to fail. Fatal errors are also known as critical errors and are caused mainly by:

  • A call to an undefined function in your program code.

  • A call to an undefined class.

Fatal and syntax errors are grave because they will bring down your program.

Fatal errors occur:

  • At the program’s installation, which makes it fail. This is known as the fatal error at startup.

  • At the compilation due to the programmer using data that the code could not find. This is known as the fatal error at compile-time.

  • At the time of running the program. The program will stop running entirely at some point. This is known as the runtime fatal error.

<?php
// this error will make the script to end abruptly
//when encountered
echo "this is runtime fatal error causing code to end";
$trial = my($data);
echo $trial;
echo "This won't display";
?>

How to mitigate against errors in your code

While errors are inevitable, they can be avoided or reduced to the barest minimum.

Here are a few suggestions:

  • Use IDEs that can do error highlighting for the language you are writing on.

  • Add helpful extensions to your code editor to help you write neat and easy-to-read codes.

  • Typos are very common. Try to type your codes more carefully.

  • Adopt professional coding standards to avoid junking out your codes.

  • Take a good look at your scripts after some time of coding. Try to do this at intervals.

RELATED TAGS

php
webdevelopment
bug
communitycreator
RELATED COURSES

View all Courses

Keep Exploring