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 Index

On PHP strings, a positive index starts at zero at the beginning of the string; and a negative index starts at the end of the string.

Negative index was added in version 7.1.

On an array, negative indexes are supported, without any special meaning: they are only available when provided. Their behavior changed when using automatic indexing though.

<?php

    $string = 'abcd';
    
    echo $string[1]; // b
    echo $string[-1]; // c

?>

Documentation

See Also