#49951 [Opn->Csd]: substr() unexpected result when length is NULL
| From: | rquadling@php.net | Date: | Thu, 22 Oct 2009 15:54:05 +0000 |
| Subject: | #49951 [Opn->Csd]: substr() unexpected result when length is NULL | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-2991@lists.php.net to get a copy of this message | ||
ID: 49951
Updated by: rquadling@php.net
Reported By: error at ioerror dot us
-Status: Open
+Status: Closed
Bug Type: Documentation problem
Operating System: Linux
PHP Version: 5.2.11
New Comment:
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.
Thank you for the report, and for helping us make our documentation
better.
Previous Comments:
------------------------------------------------------------------------
[2009-10-22 15:53:26] svn@php.net
Automatic comment from SVN on behalf of rquadling
Revision: http://svn.php.net/viewvc/?view=revision&revision=289854
Log: substr.xml() length of 0/false/null will return an empty string.
Fix bug#49951
------------------------------------------------------------------------
[2009-10-22 04:43:37] error at ioerror dot us
Description:
------------
The documentation for substr() does not seem to adequately describe
PHP's behavior when the length parameter is not given, or is FALSE or
NULL. When length is not given, the entire string, beginning from the
position specified by start, is returned.
With most PHP functions, using NULL in place of a parameter means that
it will be treated as if the parameter were not given at all; substr()
does not seem to obey this, and this fact is not documented. Instead of
acting as if length were not given, it acts as if length were 0, and
returns an empty string.
Additionally, if length is FALSE, it is treated as if length were given
as 0, and an empty string returned again. This was surprising behavior;
for instance, it prevented me from using a call to strpos() as the third
parameter to substr() and getting the desired result when strpos()
returns FALSE, that is, the entire string instead of an empty string,
which was the actual result.
I filed this as "Documentation problem" because I would like to see the
documentation reconciled with PHP's actual behavior. But perhaps the
NULL issue, at least, is something that should be fixed in PHP?
Reproduce code:
---------------
var_dump(substr("hello", 0));
var_dump(substr("hello", 0, 0));
var_dump(substr("hello", 0, NULL));
var_dump(substr("hello", 0, FALSE));
var_dump(substr("hello", 0, strpos("hello", "?")));
Expected result:
----------------
string(5) "hello"
string(0) ""
string(5) "hello"
string(5) "hello"
string(5) "hello"
Actual result:
--------------
string(5) "hello"
string(0) ""
string(0) ""
string(0) ""
string(0) ""
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=49951&edit=1