3v4l.org

run code in 300+ PHP versions simultaneously
<?php function compareCombinations($toFind, $toTry){ $nb = 4; $goodPlace = 0; $wrongPlace = 0; $toFind = str_split($toFind); $toTry = str_split($toTry); //Look for good places for($i = 0; $i < $nb; $i++){ if($toFind[$i] === $toTry[$i]){ $goodPlace++; } } //Look for wrong places for($i = 0; $i < $nb; $i++){ for($j = $i+1; $j < $nb; $j++){ if($toFind[$i] === $toTry[$j]){ $wrongPlace++; } } } return array( 'good' => $goodPlace, 'wrong' => $wrongPlace, 'absent' => $nb - $goodPlace - $wrongPlace ); } foreach(array('0000' => '0001') as $find => $try){ $r = compareCombinations($find,$try); var_dump(array( 'find' => $find, 'try' => $try ) + $r); }

preferences:
39.59 ms | 402 KiB | 5 Q