note 43061 deleted from function.substr by nlopess
| From: | nlopess@php.net | Date: | Sat, 03 Jul 2004 12:04:59 +0000 |
| Subject: | note 43061 deleted from function.substr by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-72408@lists.php.net to get a copy of this message | ||
Note Submitter: drane
----
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)