Negative Offset With Strings¶
Negative offsets on strings were introduced in PHP 7.1. They allow accessing individual characters, starting from the end of the string, rather than from the beginning.
PHP code¶
<?php
$string = 'abc';
echo $string[-1]; // c
echo $string[1]; // a
?>
Before¶
PHP Notice: Uninitialized string offset: -1
Notice: Uninitialized string offset: -1
b
After¶
cb
PHP version change¶
This behavior changed in 7.1