- Instant help with your Php coding problems

Array and string offset access syntax with curly braces is no longer supported

Question:
How to fix the array and string offset access syntax with curly braces is no longer supported error?
Answer:
// Change the code from 
$myVar{$idx}

// to
$myVar[$idx]
Description:

If you have recently started using one of the latest versions of PHP, such as PHP 8.0 or PHP 8.1, you may encounter the following error when running old PHP code:

Fatal error: Array and string offset access syntax with curly braces is no longer supported in ...

The reason is that with PHP 7.4 the array and string offset access syntax using curly braces is deprecated and it was removed entirely in PHP 8.0

Solution

Fortunately, the solution is straightforward. Just replace the curly braces { } with square brackets [ ] .

// Instead of:
$array{0};
$array{"key"};

// Write:
$array[0];
$array["key"];

Interesting facts

The original plan was to deprecate the use of curly braces in PHP 5.1, but at the last minute, the deprecated classification was removed. See PHP RFC

 

Share "How to fix the array and string offset access syntax with curly braces is no longer supported error?"
Tags:
offset access, deprecated, removed, php 7.4, php 8.0, curly braces, square bracket
Technical term:
Array and string offset access syntax with curly braces is no longer supported