in PHP

Array and String Offset Access Syntax With Curly Braces is Deprecated

After upgrading my apps to PHP 7.4, I got a warning message were like the title of this post. If you look at Stackoverflow and the other results of google search, you’ll get some thing like this,

String offsets and array elements could be accessed by curly braces {} prior to PHP 7.4.0:

$string = 'abc';
echo $string{0};  // a

$array = [1, 2, 3];
echo $array{0};  // 1

This has been deprecated since PHP 7.4.0 and generates a warning:

Deprecated: Array and string offset access syntax with curly braces is deprecated

You must use square brackets [] to access string offsets and array elements:

$string = 'abc';
echo $string[0];  // a

$array = [1, 2, 3];
echo $array[0];  // 1

All of it only explain about accessing String or Array elements. Yet I got error from code like this

$compile_array = 'cihuy';
$aa = 'array';
echo $compile_{$aa};

After thinking like Sherlock Holmes for a second, actually you can code this:

$compile_array = 'cihuy'; 
$aa = 'array'; 
echo ${'compile_' . $aa}; 

duh

Write a Comment

Comment