#49951 [NEW]: substr() unexpected result when length is NULL
| From: | error at ioerror dot us | Date: | Thu, 22 Oct 2009 04:43:37 +0000 |
| Subject: | #49951 [NEW]: substr() unexpected result when length is NULL | ||
| Groups: | php.doc.bugs | ||
| Request: | Send a blank email to doc-bugs+get-2986@lists.php.net to get a copy of this message | ||
From: error at ioerror dot us
Operating system: Linux
PHP version: 5.2.11
PHP Bug Type: Documentation problem
Bug description: substr() unexpected result when length is NULL
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 bug report at http://bugs.php.net/?id=49951&edit=1
--
Try a snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=49951&r=trysnapshot52
Try a snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=49951&r=trysnapshot53
Try a snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=49951&r=trysnapshot60
Fixed in SVN: http://bugs.php.net/fix.php?id=49951&r=fixed
Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=49951&r=needdocs
Fixed in release: http://bugs.php.net/fix.php?id=49951&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=49951&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=49951&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=49951&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=49951&r=support
Expected behavior: http://bugs.php.net/fix.php?id=49951&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=49951&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=49951&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=49951&r=globals
PHP 4 support discontinued: http://bugs.php.net/fix.php?id=49951&r=php4
Daylight Savings: http://bugs.php.net/fix.php?id=49951&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=49951&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=49951&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=49951&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=49951&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=49951&r=mysqlcfg