Array Usage With String Initialisation¶
String and arrays share the same syntax when using integer index: it accesses one element in the array or string, to reading or modifying.
On the other hand, strings do not accept string as index. In older PHP versions, PHP would convert the string to an integer, and apply the operation. In PHP 8.0, it is now forbidden to use those index, unless the string can be converted to an integer.
PHP code¶
<?php
$s = 'abc';
$s[3] = 3;
$s['4'] = '4';
print $s;
$s['c'] = 3;
?>
Before¶
abc34PHP Warning: Illegal string offset 'c' in /codes/array_with_string_initialisation.php on line 7
Warning: Illegal string offset 'c' in /codes/array_with_string_initialisation.php on line 7
After¶
abc34PHP Fatal error: Uncaught TypeError: Cannot access offset of type string on string in /codes/array_with_string_initialisation.php:7
Stack trace:
#0 {main}
thrown in /codes/array_with_string_initialisation.php on line 7
Fatal error: Uncaught TypeError: Cannot access offset of type string on string in /codes/array_with_string_initialisation.php:7
Stack trace:
#0 {main}
thrown in /codes/array_with_string_initialisation.php on line 7
PHP version change¶
This behavior changed in 8.0