note 924 deleted from function.usort by irc-html

From: Date: Sat, 19 Jan 2002 22:16:18 +0000
Subject: note 924 deleted from function.usort by irc-html
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-25517@lists.php.net to get a copy of this message
On Solaris, usort cannot currently be used for a non-deterministic comparison function. The comparison function must return the same order for the same set of data every time. A non-deterministic comparison *will* cause the webserver to segfault. This is not a bug, but rather an effect of the Solaris libc implementation. This most common use of a non deterministic comparison function is a function that randomly returns -1 or 1 to randomly shuffle an array. PHP's shuffle function also has the same problem. To get around this, you should implement your shuffle in PHP using the function below: <? function myshuffle($array) { mt_srand((double) microtime()*1000000); $num = count($array); for ($i=0; $i<$num-1; $i++) { $n = mt_rand($i+1,$num); $temp = $array[$n]; $array[$n] = $array[$i]; $array[$i] = $temp; } } $array = array(1,2,3,4,5,6,7,8,9,10); myshuffle(&$array); while (list(,$var)=each($array)) { echo $var . " "; } ?>

« previous php.notes (#25517) next »