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'

Warning: Illegal string offset 'c'

After

abc34PHP Fatal error:  Uncaught TypeError: Cannot access offset of type string on string

Fatal error: Uncaught TypeError: Cannot access offset of type string on string

PHP version change

This behavior changed in 8.0

Error Messages