Saturday, 10 September 2016

What are the different types of Errors in PHP?

Types of error
Basically there are four types of errors in PHP, which are as follows:
  • Parse Error (Syntax Error)
  • Fatal Error
  • Warning Error
  • Notice Error
Please use error_reporting(E_ALL); to get display all types of errors.
1. Parse Errors (syntax errors)
The parse error occurs if there is a syntax mistake in the script; the output is Parse errors. A parse error stops the execution of the script. There are many reasons for the occurrence of parse errors in PHP. The common reasons for parse errors are as follows:
Common reason of syntax errors are:
  • Unclosed quotes
  • Missing or Extra parentheses
  • Unclosed braces 
  • Missing semicolon

echo "Champion";
echo "Dusk"
echo "Light";
In the above code we missed the semicolon in the second line. When that happens there will be a parse or syntax error which stops execution of the script, as follows:
Parse error: syntax error, unexpected T_ECHO, expecting ',' or ';' in C:\dev\apache\apache2.2.8\htdocs\testing.php on line 3

2. Fatal Errors
Fatal errors are caused when you're asking PHP to do, that can't be done. Fatal errors stop the execution of the script. If you are trying to access the undefined functions, then the output is a fatal error.
echo "Fatal Error !!";
In the above code we called function fun2 which is not defined. So a fatal error will be produced that stops the execution of the script. Like as follows:
Fatal error: Call to undefined function fun2() in C:\dev\apache\apache2.2.8\htdocs\testing.php on line 2

3. Warning Errors
Warning errors will not stop execution of the script. The main reason for warning errors are to include a missing file or using the incorrect number of parameters in a function.

include ("Welcome.php");
echo "You got Warning Error!!";
In the above code we include a welcome.php file, however the welcome.php file does not exist in the directory. So there will be a warning error produced but that does not stop the execution of the script i.e. you will see a message Warning Error !!. Like as follows:
Warning: include(Welcome.php) [function.include]: failed to open stream: No such file or directory in C:\dev\apache\apache2.2.8\htdocs\testing.php on line 2

Warning: include() [function.include]: Failed opening 'Welcome.php' for inclusion (include_path='.;C:\php5\pear') in C:\dev\apache\apache2.2.8\htdocs\testing.php on line 2
You got Warning Error!! 

4. Notice Errors
Notice that an error is the same as a warning error i.e. in the notice error execution of the script does not stop. Notice that the error occurs when you try to access the undefined variable, then produce a notice error.

echo $test;
echo "You got Notice error!";


In the above code we tried to print a variable which named $test, which is not defined. So there will be a notice error produced but execution of the script does not stop, you will see a message  "You got Notice error!". Like as follows:
Notice: Undefined variable: test in C:\dev\apache\apache2.2.8\htdocs\testing.php on line 2
You got Notice error! 

No comments:

Post a Comment

Your comment is so valuable as it would help me in my growth of knowledge.