Parse / Syntax Error in PHP
What is meant by parse / syntax error in php?
Explanation
Parse Error is nothing but a syntax error. A parse error stops the execution of the script. The common reasons for parse errors are as follows:
Unclosed quotes
This error occurs due to missing of opening (or) closing quotes.
Example Code :<?php
echo "Wild;
echo "Animal"
echo "Lion";
?>
Result : Parse error: syntax error, unexpected 'Animal' (T_STRING), expecting ',' or ';' in /opt/lampp/htdocs/Demo/sampletest.php on line 3
In the above example, in line 2 the double quotes are not closed properly.
Missing or Extra parenthesis
This error occurs due to missing or extra parenthesis.
Example Code :<?php
$num=10;
if($num>10
{
echo "Greater";
}
else{
echo "Lesser";
}
?>
Result : Parse error: syntax error, unexpected '{' in /opt/lampp/htdocs/Demo/sampletest.php on line 4
In the above example, parse error occurs due to parenthesis are not closed properly for "if" condition.
Unclosed braces
This error occurs due to unclosed curly brace.
Example Code :<?php
$num=10;
if($num>10)
{
echo "Greater";
}
else{
echo "Lesser";
?>
Result : Parse error: syntax error, unexpected end of file in /opt/lampp/htdocs/Demo/sampletest.php on line 10
Here, syntax error occurs due to missing of closing curly brace at line number 9.
Missing semicolon
This error occurs due to missing of semicolon.
Example Code :<?php
echo "Wild";
echo "Animal"
echo "Lion";
?>
Result :Parse error: syntax error, unexpected 'echo' (T_ECHO), expecting ',' or ';' in /opt/lampp/htdocs/Demo/sampletest.php on line 4
In the above example, there is a syntax error in 3rd line (here semicolon is missing). Hence parse error stops the execution of the script and displays output like above.