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

Description

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

Example

<?php

echo 'bar'[1]; // display a

?>

Solutions

  • Upgrade to PHP 8.0 or more.

  • Put the string in a variable first.