syntax error, unexpected token “{”

Description

A syntax error is a generic error, where PHP could not compile the code. Here are some classic typo or seemingly valid code that leads to this error.

  • Since PHP 8.4, array syntax with curly braces are not valid anymore. It used to yield a warning, and it is now totally abandoned.

  • A missing closing parenthesis in a crowded parenthesis space, leads to an error in the next block.

Example

<?php

// the curly bracket array syntax has been removed
$a{1} = 2;

// curly brackets start a new block, and blocks are not allowed inside expressions (the && )
$a == 1 && {1} : 3;

?>

Solutions

  • Move to the square bracked array syntax only.

  • Add the missing closing parenthesis to the if() statement.