php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #50402 substr_compare emits no warning if length is equal to length of main_str
Submitted: 2009-12-07 20:54 UTC Modified: 2009-12-08 18:29 UTC
From: jcsahnwaldt at gmail dot com Assigned:
Status: Closed Package: Documentation problem
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: jcsahnwaldt at gmail dot com
New email:
PHP Version: OS:

 

 [2009-12-07 20:54 UTC] 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
---



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-12-08 09:29 UTC] 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-08 09:29 UTC] 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 UTC] 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 18:29 UTC] jcsahnwaldt at gmail dot com
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
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Thu Oct 08 09:00:02 2026 UTC