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 :
- 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
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.
Now run this file. It will show the notice like below as a result.
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.
Now execute the file you will get an output like below as a result of it.
We can see the example of notice over here.
In my previous article we have seen that while using include() we will get a warning at the file is not available.
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.
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.
Now run this code it will not show any warning over there.
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.
Now execute the above script. It will throw a fatal error like below as a result.
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.
Now execute the code. You will not get any fatal error.
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.
Now execute above code we will get an error like below.
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.
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.