#50402 [Csd]: substr_compare emits no warning if length is equal to length of main_str
| From: | jcsahnwaldt at gmail dot com | Date: | Tue, 08 Dec 2009 18:29:32 +0000 |
| Subject: | #50402 [Csd]: substr_compare emits no warning if length is equal to length of main_str | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-3429@lists.php.net to get a copy of this message | ||
ID: 50402
User updated by: jcsahnwaldt at gmail dot com
Reported By: jcsahnwaldt at gmail dot com
Status: Closed
Bug Type: Documentation problem
PHP Version: Irrelevant
New Comment:
For PHP 5.3.0 and 5.3.1, the documentation is now correct.
In PHP 5.2.4, substr_compare emits a warning and returns false if the
sum of 'effective' offset (i.e., after adding string length to a
negative offset) and length is greater than main string length.
See test.php below and the results for running it with PHP 5.2.4, 5.3.0
and 5.3.1.
----------------
test.php:
<?php
function test( $offset, $length )
{
echo "substr_compare('aa', 'a', $offset, $length)", PHP_EOL;
substr_compare('aa', 'a', $offset, $length);
}
test(-2, -1);
test(-2, 0);
test(-2, 1);
test(-2, 2);
test(-2, 3);
test(-1, -1);
test(-1, 0);
test(-1, 1);
test(-1, 2);
test(-1, 3);
test(0, -1);
test(0, 0);
test(0, 1);
test(0, 2);
test(0, 3);
test(1, -1);
test(1, 0);
test(1, 1);
test(1, 2);
test(1, 3);
test(2, -1);
test(2, 0);
test(2, 1);
test(2, 2);
test(2, 3);
----------------
Result with PHP 5.2.4:
substr_compare('aa', 'a', -2, -1)
PHP Warning: substr_compare(): The length must be greater than zero in
/tmp/test.php on line 5
substr_compare('aa', 'a', -2, 0)
PHP Warning: substr_compare(): The length must be greater than zero in
/tmp/test.php on line 5
substr_compare('aa', 'a', -2, 1)
substr_compare('aa', 'a', -2, 2)
substr_compare('aa', 'a', -2, 3)
PHP Warning: substr_compare(): The length cannot exceed initial string
length in /tmp/test.php on line 5
substr_compare('aa', 'a', -1, -1)
PHP Warning: substr_compare(): The length must be greater than zero in
/tmp/test.php on line 5
substr_compare('aa', 'a', -1, 0)
PHP Warning: substr_compare(): The length must be greater than zero in
/tmp/test.php on line 5
substr_compare('aa', 'a', -1, 1)
substr_compare('aa', 'a', -1, 2)
PHP Warning: substr_compare(): The length cannot exceed initial string
length in /tmp/test.php on line 5
substr_compare('aa', 'a', -1, 3)
PHP Warning: substr_compare(): The length cannot exceed initial string
length in /tmp/test.php on line 5
substr_compare('aa', 'a', 0, -1)
PHP Warning: substr_compare(): The length must be greater than zero in
/tmp/test.php on line 5
substr_compare('aa', 'a', 0, 0)
PHP Warning: substr_compare(): The length must be greater than zero in
/tmp/test.php on line 5
substr_compare('aa', 'a', 0, 1)
substr_compare('aa', 'a', 0, 2)
substr_compare('aa', 'a', 0, 3)
PHP Warning: substr_compare(): The length cannot exceed initial string
length in /tmp/test.php on line 5
substr_compare('aa', 'a', 1, -1)
PHP Warning: substr_compare(): The length must be greater than zero in
/tmp/test.php on line 5
substr_compare('aa', 'a', 1, 0)
PHP Warning: substr_compare(): The length must be greater than zero in
/tmp/test.php on line 5
substr_compare('aa', 'a', 1, 1)
substr_compare('aa', 'a', 1, 2)
PHP Warning: substr_compare(): The length cannot exceed initial string
length in /tmp/test.php on line 5
substr_compare('aa', 'a', 1, 3)
PHP Warning: substr_compare(): The length cannot exceed initial string
length in /tmp/test.php on line 5
substr_compare('aa', 'a', 2, -1)
PHP Warning: substr_compare(): The length must be greater than zero in
/tmp/test.php on line 5
substr_compare('aa', 'a', 2, 0)
PHP Warning: substr_compare(): The length must be greater than zero in
/tmp/test.php on line 5
substr_compare('aa', 'a', 2, 1)
PHP Warning: substr_compare(): The length cannot exceed initial string
length in /tmp/test.php on line 5
substr_compare('aa', 'a', 2, 2)
PHP Warning: substr_compare(): The length cannot exceed initial string
length in /tmp/test.php on line 5
substr_compare('aa', 'a', 2, 3)
PHP Warning: substr_compare(): The length cannot exceed initial string
length in /tmp/test.php on line 5
----------------
Result with PHP 5.3.0 and 5.3.1:
substr_compare('aa', 'a', -2, -1)
Warning: substr_compare(): The length must be greater than zero in
/tmp/test.php on line 5
substr_compare('aa', 'a', -2, 0)
Warning: substr_compare(): The length must be greater than zero in
/tmp/test.php on line 5
substr_compare('aa', 'a', -2, 1)
substr_compare('aa', 'a', -2, 2)
substr_compare('aa', 'a', -2, 3)
substr_compare('aa', 'a', -1, -1)
Warning: substr_compare(): The length must be greater than zero in
/tmp/test.php on line 5
substr_compare('aa', 'a', -1, 0)
Warning: substr_compare(): The length must be greater than zero in
/tmp/test.php on line 5
substr_compare('aa', 'a', -1, 1)
substr_compare('aa', 'a', -1, 2)
substr_compare('aa', 'a', -1, 3)
substr_compare('aa', 'a', 0, -1)
Warning: substr_compare(): The length must be greater than zero in
/tmp/test.php on line 5
substr_compare('aa', 'a', 0, 0)
Warning: substr_compare(): The length must be greater than zero in
/tmp/test.php on line 5
substr_compare('aa', 'a', 0, 1)
substr_compare('aa', 'a', 0, 2)
substr_compare('aa', 'a', 0, 3)
substr_compare('aa', 'a', 1, -1)
Warning: substr_compare(): The length must be greater than zero in
/tmp/test.php on line 5
substr_compare('aa', 'a', 1, 0)
Warning: substr_compare(): The length must be greater than zero in
/tmp/test.php on line 5
substr_compare('aa', 'a', 1, 1)
substr_compare('aa', 'a', 1, 2)
substr_compare('aa', 'a', 1, 3)
substr_compare('aa', 'a', 2, -1)
Warning: substr_compare(): The length must be greater than zero in
/tmp/test.php on line 5
substr_compare('aa', 'a', 2, 0)
Warning: substr_compare(): The length must be greater than zero in
/tmp/test.php on line 5
substr_compare('aa', 'a', 2, 1)
Warning: substr_compare(): The start position cannot exceed initial
string length in /tmp/test.php on line 5
substr_compare('aa', 'a', 2, 2)
Warning: substr_compare(): The start position cannot exceed initial
string length in /tmp/test.php on line 5
substr_compare('aa', 'a', 2, 3)
Warning: substr_compare(): The start position cannot exceed initial
string length in /tmp/test.php on line 5
Previous Comments:
------------------------------------------------------------------------
[2009-12-08 09:29:41] 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.
------------------------------------------------------------------------
[2009-12-08 09:29:31] svn@php.net
Automatic comment from SVN on behalf of rquadling
Revision: http://svn.php.net/viewvc/?view=revision&revision=291868
Log: Corrected initializer for length.
Expanded return values.
Bug#50402
------------------------------------------------------------------------
[2009-12-08 09:29:21] rquadling@php.net
It seems that this should read ...
"If offset is equal to or greater than the length of main_str or if
length is set and is less than 1, ..."
As offsets start at 0, an offset whose value is the length of the
string is actually beyond the string.
php -r "echo substr_compare('abcd', 'abcd', 4);"
Warning: substr_compare(): The start position cannot exceed initial
string length in Command line code on line 1
The error relates to "offset", not "length".
------------------------------------------------------------------------
[2009-12-07 20:54:23] jcsahnwaldt at gmail dot com
Description:
------------
Incorrect: "If length is equal or greater than length of main_str"
Correct: "If length is greater than length of main_str"
Also see bug 37394
Reproduce code:
---------------
---
From manual page: function.substr-compare#Return Values
---
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=50402&edit=1