syntax error, unexpected token “if”, expecting “)”

Description

If-then commands are standalone commands. They can’t be part of another expression. Here, PHP found the if, while it was expecting another expression.

Then, PHP mentions that it was expecting ): this usually hints at an unclosed method call syntax. In the example here, the previous line holds an unclosed function call to foo.

Example

<?php

$a = foo(1

if ($a == 2) {
    print OK;
}

?>

Solutions

  • Look for the previous opening ( parenthesis in functioncalls and check that it is balanced.

  • Look for all previous openings ( parenthesis in functioncalls and check that they are all balanced.