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

Cannot use string offset as an object

Description

It is possible to use the array syntax $s[$i] on a string, to access an individual character. And in general, it is also possible to chain the array syntax to access properties $array[1]->p.

Yet, the individual characters of a string are never objects, so it is not possible to use that syntax with them, while reading it from the string.

Example

<?php

$string = '123';

unset($string[0]->a);

?>

Alternatives

  • Remove the 2nd array syntax when working with a string.