substr() Returns Empty String On Out Of Bond Offset¶
substr() used to return false when the parameters used to extract the string were out of bound, or well out of the string sizes. With PHP 8.0, this is not reported as an error anymore, and fails silently.
One collateral impact is that code that checks on the returned value to be false is now dead code.
PHP code¶
<?php
var_dump(substr('FooBar', 42, 3)); // "" in PHP >=8.0, false in PHP < 8.0
var_dump(mb_substr('FooBar', 42, 3)); // "" in PHP >=8.0, false in PHP < 8.0);
var_dump(iconv_substr('FooBar', 42, 3)); // "" in PHP >=8.0, false in PHP < 8.0);
var_dump(grapheme_substr('FooBar', 42, 3)); // "" in PHP >=8.0, false in PHP < 8.0);
?>
Before¶
bool(false)
string(0) ""
bool(false)
bool(false)
After¶
string(0) ""
string(0) ""
string(0) ""
string(0) ""
PHP version change¶
This behavior changed in 8.0