null For The Rest

<?php

$string = 'abcdef';

echo substr($string, 2, 3).PHP_EOL; // cde
echo substr($string, 2, 0).PHP_EOL; // (nothing)

echo substr($string, 2, 10).PHP_EOL; // the rest
echo substr($string, 2, null).PHP_EOL; // the rest
echo substr($string, 2).PHP_EOL; // the rest

The third argument of substr() is the length of the string that needs to be included. It counts the number of characters.

This value may be a positive integer, for an actual length, a negative integer for the length of the string minus that value, or 0 for nothing. That last one may be strange by itself, but makes sense for continuity of the feature.

Finally, a very long length captures the rest of the string, just like omitting the value altogether, or, even, passing null. In that case, null and 0 are not the same feature.

This does not apply to the second argument of substr(), which is non nullable, and must be an integer.

All this applies, consistently, to other functions that manage ranges, such as array_slice().

See Also

PHP Features

Last updated: 18 August 2026