Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

syntax error, unexpected token “if”

Description

If-then expressions are standalone expressions, and cannot be used inside another expression. Hence, they must be preceded by a semi-colon, or any other expression closing character.

Example

<?php

$a = foo() && if ($a == 2) { echo $a; } else { echo "No A"; }

match( 
if ($a == 2) { echo $a; } else { echo "No A"; }

?>

Alternatives

  • Replace the && by a semi-colon.
  • Move the $a = foo() && inside the if condition, before the $a == 2.
  • Check for opening parenthesis, and make sure they are closed.
  • Check for previous statements that uses parenthesis, such as match, switch, if… and make sure they are closed.