php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #29907 stange str comparing results
Submitted: 2004-08-31 08:48 UTC Modified: 2004-09-02 08:38 UTC
From: benjamin dot lewis at samail dot net Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 4.3.7 OS: redhat
Private report: No CVE-ID: None
 [2004-08-31 08:48 UTC] benjamin dot lewis at samail dot net
Description:
------------
cases length and value give the expected results, but when mixed in the case of length_and_value it gives unexpected results.

Sorry for posting more than 20 lines i don't have an open server to post it to.

Ben

Reproduce code:
---------------
<?php

$doesntwork = array(
'd1_b_000_000_0_b_000_001_0_b_000_001_0', 
'd1_b_000_000_0_b_000_001_0_b_001_001_0', 
'd1_b_000_000_0_b_000_001_0_b_000_002_0', 
'd1_b_000_000_0_b_000_001_0_b_001_002_0', 
'd1_b_000_000_0_b_000_001_0_b_001_003_0', 
'd1_b_000_000_0_b_000_001_0_b_000_004_0_b_000_000_0', 
'd1_b_000_000_0_b_000_001_0_b_000_004_0_b_000_001_0', 
'd1_b_000_000_0_b_000_001_0_b_000_004_0_b_000_002_0', 
'd1_b_000_000_0_b_000_001_0_b_000_005_0_b_000_001_0_b_000_001_0', 
'd1_b_000_000_0_b_000_001_0_b_000_005_0_b_000_001_0_b_002_001_0_b_000_000_0', 
'd1_b_000_000_0_b_000_001_0_b_000_005_0_b_000_001_0_b_002_001_0_b_000_001_0', 
'd1_b_000_000_0_b_000_001_0_b_000_005_0_b_000_001_0_b_002_001_0_b_000_002_0', 
'd1_b_000_000_0_b_000_001_0_b_000_005_0_b_000_001_0_b_000_002_0' ,
'd1_b_000_000_0_b_000_001_0_b_000_005_0_b_000_001_0_b_002_002_0_b_000_000_0', 
'd1_b_000_000_0_b_000_001_0_b_000_005_0_b_000_001_0_b_002_002_0_b_000_001_0', 
'd1_b_000_000_0_b_000_001_0_b_000_005_0_b_000_001_0_b_002_002_0_b_000_002_0', 
'd1_b_000_000_0_b_000_001_0_b_000_005_0_b_000_001_0_b_000_003_0', 
'd1_b_000_000_0_b_000_001_0_b_000_005_0_b_000_001_0_b_000_004_0'
);

function value($a, $b)
{
	if($a < $b)	return -1;
	else								return 1;
}

function length($a, $b)
{
	if(strlen($a) < strlen($b))	return -1;
	else								return 1;
}

function length_and_value($a, $b)
{
	if(strlen($a) < strlen($b) || $a < $b)	return -1;
	else								return 1;
}

if(is_array($doesntwork))
{
	foreach($doesntwork as $id) echo '<br>'.$id;
	echo '<hr>';

	usort($doesntwork, 'length');

	foreach($doesntwork as $id) echo '<br>'.$id;
	echo '<hr>';

	usort($doesntwork, 'value');

	foreach($doesntwork as $id) echo '<br>'.$id;
	echo '<hr>';

	usort($doesntwork, 'length_and_value');

	foreach($doesntwork as $id) echo '<br>'.$id;
	echo '<hr>';
}

?>


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-08-31 08:50 UTC] benjamin dot lewis at samail dot net
usort
 [2004-08-31 08:53 UTC] tony2001@php.net
Try to reduce the code so we can understand the problem without having to read huge and tangled scripts.
Btw, 4.3.7 is not the current stable version, so you'd better update it.
 [2004-09-02 05:47 UTC] benjamin dot lewis at samail dot net
the problem is with sorting  with usort where the result of sorting with the length_and_depth function which uses an OR in the condition, gives strange results conpared with sorting on the two conditions alone.
 [2004-09-02 08:38 UTC] derick@php.net
Not a bug in PHP then, marking as Bogus.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Sep 18 21:01:26 2024 UTC