Array and string offset access syntax with curly braces is deprecated¶
Description¶
PHP used to support the curly braces {}
to access elements in an array and a string. This was deprecated in PHP 7.4 and abandoned in PHP 8.0. The only operator to access an element is the square brackets []
.
Example¶
<?php
$string = 'abc';
echo $string{1}; // b
$array = ['A', 'B', 'C'];
echo $array{1}; // B
?>
Solutions¶
Switch to the square brackets.
Use the substr() function to extract one string char.
In previous PHP versions, this error message used to be Array and string offset access syntax with curly braces is no longer supported.
In more recent PHP versions, this error message is now syntax error, unexpected token “{”, expecting “,” or “;”.
Changed Behavior¶
This error may appear following an evolution in behavior, in previous versions. See curly_braces.
Changed Behavior¶
This error may appear following an evolution in behavior, in previous versions. See curly_braces.