phperrors

PHP Error Types And Their Differences

Would you like being treated as dumb in coding by making an error(i.e PHP error) in code? Does nobody want to look like an idiot right? Huh.

If you’re aware of the competition, you might understand the importance of being perfected, it’s almost impossible to possess a good career with a prestigious organization without awesome skills.
Before we go ahead, it’s important to plan your coding action right? Exactly, without planning nothing pays off.

Someone Once said “If you fail to plan, You Plan To Fail” : Frankly speaking it was not written by me. 🙂 Just Kidding!

Once we’re ready with a set of actions, the next step would be to find errors and tackle them carefully while coding. Below are the four most common errors you’ll find while working on PHP.

PHP Error Types :

  • Notice
  • Warning
  • Fatal Error
  • Parse Error

Note : It’s a better way to have a error_reporting on while doing programming. It might be possible that display errors could be turned off in the php.ini or your Apache config file however you can turn it on by adding below lines at the top of the PHP file.
error_reporting(E_ALL);  //Set the error reporting level
ini_set(‘display_errors’, 1); //This will overwrite the php.ini params value

Notices :

This is the most common error which will encounter widely while writing a program or a script.

Example :  if we will try to access a variable which is not defined yet.

If we want to see this kind of errors while doing code, then we should have to add the code which I have mentioned in my note because sometimes it might be possible that these options have different settings in php.ini file.

Code :

<?php 
error_reporting(E_ALL);  
ini_set('display_errors', 1);
echo $value;
echo "<br/><strong>We can see the example of notice over here.</strong>";
?>

Now run this file. It will show the notice like below as a result.

PHP error-notice

Here you can see that we will get this kind of notice message if we are not initializing the variable. Now we see how to avoid this kind of notice while performing code.

Code :

<?php 
error_reporting(E_ALL);  
ini_set('display_errors', 1);
$value='';
echo $value;
echo "<br/><strong>We can see the example of notice over here.</strong>";
?>

Now execute the file you will get an output like below as a result of it.

Output :

We can see the example of notice over here.

Warning :

In my previous article we have seen that while using include() we will get a warning at the file is not available.

Code :

<?php 
error_reporting(E_ALL);  
ini_set('display_errors', 1);
include("Helper1.php");
echo "<br><br> &nbsp;&nbsp;This is Index File";
?>

if include() is not able to find the specific file on specified location it will throw the warning like below, however, it will not stop the execution of the current script.

PHP error-warning

So in above image, we can easily identify how warning will come.  Now we will see how can we avoid this kind of warning by doing smart code.

Code :

<?php 
error_reporting(E_ALL);  
ini_set('display_errors', 1);
if(file_exists('Helper1.php'))
{
     include("Helper1.php");
}
echo "<br><br> &nbsp;&nbsp;This is Index File.";
?>

Now run this code it will not show any warning over there.

Output :

This is Index File.

Fatal Error :

A fatal error is a critical error. It will stop the execution of code as a result.  For example,  calling a non-existent function or initiate an object of the nonexistent class or we can try to call a file which is not located at a specified location.

Code :

<?php 
error_reporting(E_ALL);  
ini_set('display_errors', 1);
getMessage();
echo "This code have fatal error code";
?>

Now execute the above script. It will throw a fatal error like below as a result.

PHP error-fatal

Here we can see that we are getting a fatal error like “Call to undefined function“. In our above code we have not defined “getMessage()” anywhere, so it will stop the execution of code. Now how we can prevent this kind of error while programming.

Code :

<?php 
error_reporting(E_ALL); 
ini_set('display_errors', 1);
if(function_exists('getMessage'))
{
   getMessage();
}
echo "Avoid errors by doing smart code.";
?>

Now execute the code. You will not get any fatal error.

Output :

Avoid errors by doing smart code.

Parse Error :

This type of error has also stopped the execution of the code. For example,  we have done any mistake in PHP code like,  missing a semicolon or any unexpected symbol in code.

Code :

<?php 
error_reporting(E_ALL); 
ini_set('display_errors', 1);
if(function_exists('getMessage')
{
   echo "parse error example.";
}
?>

Now execute above code we will get an error like below.

PHP error-parse-error

Here we can see that, we are facing the syntax error near the if condition. I just missed to add ” ) ” at the end of if condition. So once I will add ” ) ” in if condition it will display the output like below.

<?php 
error_reporting(E_ALL); 
ini_set('display_errors', 1);
if(function_exists('getMessage'))
{
   echo "Hello world!!!";
}
?>

Output :

Hello world!!!

Comprehensive Errors List :

While programming, we are facing so many errors. To make a better deal with these errors we need to understand the error type and the cause that we will face due to specific error. It will help us to resolve these kind of errors quickly.

  • E_PARSE : It shows the parse error which will occurred during compilation time. It will terminates the script execution.
  • E_ERROR : It indicates the fatal error. It will terminates the script execution.
  • E_STRICT : These are the run time errors.
  • E_NOTICE : It’s run time error which will caused due to error in code. It will not terminate the script execution.
  • E_ALL : It will catch all the errors and warnings.
  • E_USER_ERROR : It’s the user generated error message.
  • E_WARNING : It’s run time warning which will not stop the script execution.
  • E_USER_NOTICE : it’s the user generated notice message.
  • E_USER_WARNING : It’s the user generated warning message.
  • E_CORE_ERROR : It indicates fatal error. It will terminates the script execution.
  • RECOVERABLE_ERROR : These are the errors which we are able to catch.
  • E_CORE_WARNING : It will occurs while initial startup of PHP.
  • E_COMPILE_ERROR : It indicates compile time errors.

So above is the list of PHP Error Types. Hope this article helps you to understand PHP error types  properly. You can also get some basic detail of SEO over Basic SEO Checklist For Developers article.

3 thoughts on “PHP Error Types And Their Differences

Leave a Reply