syntax error, unexpected ‘[’, expecting ‘;’ or ‘,’

Description

Using the array syntax directly on an interpolated string was not possible until PHP 8.0. Until then, it reported a syntax error.

Example

<?php

$a = 'foo';
echo bar$a[4]; // display f

?>

Solutions

  • Upgrade to PHP 8.0 or more.

  • Put the string in a variable first.