PHP Parse Errors (unexpected $end in, T_IF/T_WHILE/T_FOR, T_STRING…)

You may found parse error most dangerous type of error in PHP programming.
Parse errors are reported by interpreter when any syntax error(s) are found on your web page.
You may find parse error dangerous and serious because it generally does not report correct place of error(s). But good point is that it at least report you error directly and it does not goes unreported like logical errors.
Here is a list of those errors: -
1. Parse error: syntax error, unexpected $end in … on line..
2. Parse error: syntax error, unexpected T_IF/T_WHILE/T_FOR, expecting ‘{‘ in …
3. Parse error: unexpected T_IF, expecting ‘,’ or ‘;’ in … on line ..
4. Parse error: unexpected T_STRING, expecting’,’ or ‘;’ in … on line …

Now I will directly give you example for all these errors, so that you can find problem within you code knowing reasons.
1. Parse error: syntax error, unexpected $end in
Reason: 'closing ‘}' of the first if is missing. So this is telling about the last line on page. You can use HTML block within php block php is waited till last moment.
Same error will be shown to you in case if you forgot closing brace of a function.


if (5 > 4) {
   echo 'IF ';
   if (1< 0) {
       echo 'Inner IF';
   } 

2. Parse error: syntax error, unexpected T_IF, expecting '{' in
Reason: Function opening Brace not found. So it will point error to If (). Report errors: T_IF for if, T_WHILE for while, T_FOR for.
As you can give start brace just after “function funName()” on same line or new line/lines. So interpreter was looking for that and reported error on IF (reserved keyword).


function test ()
 if (1>2) {
   echo ' IF ';
  }
  for ($i=0; $i<=5; $i++) {
    echo "For $i";
  }
}

3. Parse error: parse error, unexpected T_IF, expecting ',' or ';' in /var/www/html/testpage2.php on line …
Reason: missing ';' on first echo. So giving error on second if statement. Expecting first echo to continue, but it is not. Instead it got if statement.


if (5 > 4) {
   echo 'testing error '
   if (1< 0) {
      echo 'Inner IF';
   }
}

Similar error will occur if you use (,) instead of required (;) for line ending. Error will be: parse error, unexpected T_IF in ….


if (5 > 4) {
   echo 'parse error answer ',
   if (1< 0) {
      echo 'Inner IF';
   }
}

4. Parse error: parse error, unexpected T_STRING, expecting ',' or ';' in … on line …
Reason: First echo is not closed. missing "'" on first echo. Same error will comes if you use (") instead of (') matching quote.


if (5 > 4) {
   echo 'error due to missing quote ;
   if (1< 0) {
      echo 'Inner IF';
   }
}

5. Parse error: syntax error, unexpected T_STRING in ... on line ...
Reason: function keyword is wrong.


funtion test () {
  if (1>2) {
    echo ' error in function spelling ';
  }
  for ($i=0; $i<=5; $i++) {
    echo "For $i";
  }
}

Related:PHP Custom Error Handler
JavaScript Error Handling for Beginners
PHP Parse Errors

Comments are open for an year period. Please, write here on Facebook page.