|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2010-08-17 18:22 UTC] david at grudl dot com
Description:
------------
substr() should not return FALSE if is used with empty string. Yes, it is documented behaviour (If $string is less than or equal to $start characters long, FALSE will be returned), but it is not expected behaviour. There is no way to use "smaller $start".
Test script:
---------------
substr('x', 0, 2) // returns again 'x'
substr('', 0, 2) // returns FALSE but '' is expected
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Oct 25 04:00:01 2025 UTC |
I don't think it makes any sense to pass an empty string directly to substr and make it behave weirdly. Just wrap it inside an if statement to check if the string is empty if you are using userdata. In either case, calling sub string on an empty string would return nothing, simply casting it to a string makes it empty: $string = (string) substr('', 0, 2);Then check the return value of substr(): if(($string = substr($old_string, 0, 5)) === false) { /* error */ } There is a million ways to over come this problem, simply check the string first before trying to substring it