Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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

Extension

© 2023-2026, Damien Seguy - Exakat