I improved the code a bit and changed to use a pow
instead
$words = array('abcd', 'hello', 'dogs', 'hiya');
$newWords = array();
foreach ($words as $word) {
$len = strlen($word);
if ($len > 4) {
$len = 4;
}
$i = 0;
$j = $len - 1;
$score = 0;
while ($i < $len) {
$byte = ord($word[$i]);
$score += $byte * pow(256, $j);
$i++;
$j--;
}
$newWords[$score] = $word;
}
ksort($newWords);
print_r($newWords);
it does exactly what you expected:
Array ( [1633837924] => abcd [1685022579] => dogs [1751477356] => hello [1751742817] => hiya )
and you actually used XOR http://www.php.net/manual/en/language.operators.bitwise.php