note 43061 added to function.substr
| From: | drane at rn2 dot php dot net | Date: | Tue, 08 Jun 2004 15:22:45 +0000 |
| Subject: | note 43061 added to function.substr | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-70723@lists.php.net to get a copy of this message | ||
This post replies to these posts:
icewolf at excite dot co dot jp 26-Dec-2003 05:03
thivierr at telus dot net 12-Dec-2003 12:11
Tested on PHP 4.1.2 and PHP 4.3.1.
The documentation IS wrong. It's probably an off-by-one error because "start" counts
from 0.
It reads, "If string is less than start characters long, FALSE will be returned."
It should read, "If string is less than or equal to start characters long, FALSE will be
returned.".
I'd prefer an empty string when string length equals start, but that's just me.
I first discovered this when using substr('',0,5). (Not literally -- that empty string
was really a variable whose value happened to be an empty string.) The FALSE return value threw me
right off.
Here's a counterexample to the documentation:
$str_a="1234567890";
$str_b=substr($str_a,9,1);
$str_c=substr($str_a,10,1);
echo('<pre>');
echo('Length of $str_a: ');
echo(strlen($str_a));
echo('</pre>');
echo('<pre>');
echo('Substr, Start=9: ');
var_dump($str_b);
echo('</pre>');
echo('<pre>');
echo('Substr, Start=10: ');
var_dump($str_c);
echo('</pre>');
OUTPUT:
Length of $str_a: 10
Substr, Start=9: string(1) "0"
Substr, Start=10: bool(false)
----
Manual Page -- http://www.php.net/manual/en/function.substr.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+43061
Delete -- http://master.php.net/manage/user-notes.php?action=delete+43061&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+43061&report=yes
Search -- http://master.php.net/manage/user-notes.php