Req #52628 [Opn->Csd]: substr() returns false for empty string
| From: | kalle@php.net | Date: | Tue, 17 Aug 2010 20:19:54 +0000 |
| Subject: | Req #52628 [Opn->Csd]: substr() returns false for empty string | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-153537@lists.php.net to get a copy of this message | ||
Edit report at http://bugs.php.net/bug.php?id=52628&edit=1
ID: 52628
Updated by: kalle@php.net
Reported by: david at grudl dot com
Summary: substr() returns false for empty string
-Status: Open
+Status: Closed
Type: Feature/Change Request
Package: Strings related
PHP Version: Irrelevant
-Assigned To:
+Assigned To: kalle
Block user comment: N
New Comment:
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);
Previous Comments:
------------------------------------------------------------------------
[2010-08-17 18:22:33] 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
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=52628&edit=1