|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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 --- PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2026 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 08 09:00:02 2026 UTC |
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".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