| Bug #49951 | substr() unexpected result when length is NULL | ||||
|---|---|---|---|---|---|
| Submitted: | 22 Oct 2009 4:43am UTC | Modified: | 22 Oct 2009 3:54pm UTC | ||
| From: | error at ioerror dot us | Assigned to: | |||
| Status: | Closed | Category: | Documentation problem | ||
| Version: | 5.2.11 | OS: | Linux | ||
| Votes: | 1 | Avg. Score: | 3.0 ± 0.0 | Reproduced: | 1 of 1 (100.0%) |
| Same Version: | 0 (0.0%) | Same OS: | 1 (100.0%) | ||
[22 Oct 2009 3:53pm UTC] 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
[22 Oct 2009 3:54pm UTC] rquadling@php.net
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.

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) ""