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

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.

© 2023-2026, Damien Seguy - Exakat