Negative Index On Strings

Negative index reaches an offset in a string, starting from the last elements in that string, instead of starting from position 0.

This feature is also supported by substr(), and was introduced in PHP 7.1.

PHP code

<?php

$string = 'abc';

var_dump($string[-1]);

?>

Before

PHP Notice:  Uninitialized string offset: -1 in /codes/negativeIndex.php on line 5

Notice: Uninitialized string offset: -1 in /codes/negativeIndex.php on line 5
string(0) ""

After

string(1) "c"

PHP version change

This behavior changed in 7.1