#47298 [Bgs]: Unexpected substr return value when start is negative
| From: | janne dot salo at exove dot fi | Date: | Thu, 05 Feb 2009 12:17:25 +0000 |
| Subject: | #47298 [Bgs]: Unexpected substr return value when start is negative | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-133528@lists.php.net to get a copy of this message | ||
ID: 47298
User updated by: janne dot salo at exove dot fi
Reported By: janne dot salo at exove dot fi
Status: Bogus
Bug Type: Strings related
Operating System: CentOS 5.2
PHP Version: 5.2.8
New Comment:
Ran a few more tests, results are
4.4.8 (CentOS 4.2): "12"
4.4.9 (MacOS X Tiger): "12"
5.1.6 (CentOS 5.2): "12"
5.2.0 (Debian 4): "12"
5.2.4 (Fedora 8): FALSE
5.2.6 (RHEL4 and Ubuntu): FALSE
I would still argue there's an issue with some 5.2.x series versions
(although I admit now that "12" is probably the intended return value
since most versions return that and thus 5.2.8 works correctly and
5.2.2, 5.2.4 and 5.2.6 don't). A whole different question is if "12" is
a sensible return value, but let's not go there :)
Previous Comments:
------------------------------------------------------------------------
[2009-02-04 14:59:15] iliaa@php.net
php 5.1.2 returns 12, so there is no BC break here.
------------------------------------------------------------------------
[2009-02-04 08:40:38] janne dot salo at exove dot fi
Description:
------------
When substr is given a negative value as $start AND the absolute value
of $start is greater than the length of the string, something unexpected
occurs: $start seems to be considered equal to 0 in this case.
The documentation states: "If string is less than or equal to start
characters long, FALSE will be returned". Strict intepretation would be
that if $start is negative, FALSE will never be returned since a
negative start can never be greater than the length of the string.
However, what you would expect (and what you get in versions older than
5.2.8) is that if the ABSOLUTE value of start is greater than the length
of the string, FALSE will be returned.
Reproduce code:
---------------
substr("1234567", -9, 2);
Expected result:
----------------
The above call returns FALSE in previous PHP versions (tested with
5.2.2 in MacOS X and 5.2.6 in Fedora 8).
Actual result:
--------------
'12'. (Which corresponds to call substr("1234567", 0, 2))
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=47298&edit=1