#41375 [Bgs]: substr() undocumented behavior change from 5.2.1 to 5.2.2
| From: | chagenbu@php.net | Date: | Fri, 07 Sep 2007 18:05:14 +0000 |
| Subject: | #41375 [Bgs]: substr() undocumented behavior change from 5.2.1 to 5.2.2 | ||
| References: | 1 | Groups: | php.doc |
| Request: | Send a blank email to phpdoc+get-969378070@lists.php.net to get a copy of this message | ||
ID: 41375
Updated by: chagenbu@php.net
Reported By: bugs dot php dot net at kreidenweis dot com
Status: Bogus
Bug Type: Documentation problem
Operating System: Any
PHP Version: 5.2.2
New Comment:
Ah. I do see that now, but it's in the docs for when $length is given
and is positive. I guess the default for $length is presumed to be
positive? This should definitely be documented under the $start
parameter, not $length. And so I think it's a stretch to say this has
been documented.
If this was never *supposed* to work then I can see it's a
documentation problem and will let it go.
Previous Comments:
------------------------------------------------------------------------
[2007-09-07 17:46:01] sean@php.net
As far as I can tell, this has been documented since at least 2002. The
fact that it worked up until 5.2.1 doesn't mean that you should have
used it.
From the docs:
"""If string is less than or equal to start characters long, FALSE
will be returned."""
http://cvs.php.net/viewvc.cgi/phpdoc/en/reference/strings/functions/substr.xml?revision=1.1&view=markup
S
------------------------------------------------------------------------
[2007-09-07 17:25:21] chagenbu@php.net
I should have specified that sometimes you only want the last X
characters of a string. That's what this is about.
------------------------------------------------------------------------
[2007-09-07 16:10:25] tony2001@php.net
>if (strlen($string) > 16))
> $shorter = substr($string, -16);
>else
> $shorter = $string;
$shorter = substr($string, 0, 16);
Please don't tell me you didn't know that.
------------------------------------------------------------------------
[2007-09-07 15:50:42] chagenbu@php.net
Alright, here's the use case:
I have a string. I want at most 16 characters of it. For all PHP
versions before 5.2.[2 or 3], I could do this:
$shorter = substr($string, -16);
Now I have to do this:
if (strlen($string) > 16))
$shorter = substr($string, -16);
else
$shorter = $string;
My question is, especially since I'm asking that the historical
behavior of PHP be maintained, how does this change improve the
language? What is the point of it?
------------------------------------------------------------------------
[2007-09-07 15:36:40] tony2001@php.net
I don't think it's good idea to change it in every release back and
forth.
Also the current behavior makes much more sense to me.
Negative string length is a nonsense, so false is the correct result.
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
http://bugs.php.net/41375
--
Edit this bug report at http://bugs.php.net/?id=41375&edit=1