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 in /codes/negativeOffsetOnStrings.php on line 5

Notice: Uninitialized string offset: -1 in /codes/negativeOffsetOnStrings.php on line 5
b

After

cb

PHP version change

This behavior changed in 7.1