Req #68863 [Com]: Inconsistent return from substr()

From: Date: Fri, 30 Jan 2015 03:21:42 +0000
Subject: Req #68863 [Com]: Inconsistent return from substr()
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-190335@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=68863&edit=1 ID: 68863 Comment by: debian at michael-neubert dot de Reported by: scott at smashcat dot org Summary: Inconsistent return from substr() Status: Open Type: Feature/Change Request Package: Strings related Operating System: Linux PHP Version: 5.6.4 Block user comment: N Private report: N New Comment: As addition from the manual: http://php.net/manual/en/function.substr.php "If string is less than or equal to start characters long, FALSE will be returned." This is for the 3rd and 4th of the given examples. Previous Comments: ------------------------------------------------------------------------ [2015-01-30 03:18:37] debian at michael-neubert dot de I would not change the current behaviour. Too many PHP code is written based on that results. And the documentation is clear: substr("a",0,10); -> returns "a", I would expect that. If you do not like that, make an extra length() check before. substr("a",0,0); -> returns empty string, I epxect that, see documentation: http://php.net/manual/en/function.substr.php "If length is given and is 0, FALSE or NULL an empty string will be returned." substr("",0,10); and substr("",0,0); -> returns false, I would expect that, because the given input string is emtpy. There is no starting point, so false correct. The input variables are checked in order from the first to the last argument. So for me the behaviour is as expected and this is no bug and nothing should be changed. ------------------------------------------------------------------------ [2015-01-25 02:09:08] yohgaki@php.net I would like to have substr("a",0,10) == "a" substr("a",0,0) == "" substr("",0,10) == "" substr("",0,0) == "" It would be nicer not to return FALSE for string and return maximum chars available upto specified length. What others think? ------------------------------------------------------------------------ [2015-01-20 13:31:27] scott at smashcat dot org Description: ------------ Currently the substr() function is inconsistent in its return value. Thus the value of the String object is affecting the type of object / primitive that is returned. Although the input String (shown below) is of zero length, it's still a String. So currently the returned value type needs to be checked by the calling code. To return a consistent type of value I suggest that either the last case should return an empty string, OR the substr() function should return boolean false if the input string is shorter than the required substr. Test script: --------------- substr("a",0,10); substr("a",0,0); substr("",0,10); substr("",0,0); Expected result: ---------------- substr("a",0,10) === false substr("abc",0,1) === "a" substr("",0,0) === "" substr("",0,1) === false Actual result: -------------- substr("a",0,10) == "a" substr("a",0,0) == "" substr("",0,10) == false substr("",0,0) == false ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=68863&edit=1

« previous php.bugs (#190335) next »