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

From: Date: Sun, 25 Jan 2015 02:09:09 +0000
Subject: Req #68863 [Opn]: Inconsistent return from substr()
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-190193@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
 Updated by:         yohgaki@php.net
 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:

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?


Previous Comments:
------------------------------------------------------------------------
[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


Thread (5 messages)

« previous php.bugs (#190193) next »