I am wondering how to output the following expression in php any help would be greatly appriciated. If I have 3 random numbers that are 2, 16, and 50 stored in 3 different variables how can I compare it to another set of numbers and know that they match in any order Example: $number1 = rand(1,100) $number2 = rand(1,100) $number3 = rand(1,100) If ($number1 = 2 or 16 or 50) and ($number2 = 2 or 16 or 50) and ($number3 = 2 or 16 or 50) { echo "You found the random numbers"; } else { echo "try again!"; } I know the syntax is wrong thats what I need help with
or = || and = && if ($number1 == 2 || $number1 == 16 || $number1 == 50) && ($number2 == 2 || $number2 ==16 || $number2 ==50) && ($number3 == 2 || $number3 == 16|| $number3 ==50) { echo "You found the random numbers"; } else { echo "try again!"; } PHP: of course in this case, the user could guess any of the given numbers all three times and get the 'you found the random numbers' message. ie the dude could guess 2 for all three numbers and get the first message, not sure if that's what you wanted.
put the 2,16,50 in an array $samparray = array(2,16,50); $aaa = in_array($samparray, $yournumber); // or interchange the parameters print_r($aaa); in_array will return as boolean whether it is present in the array.
Assuming the number of numbers you will be comparing will always both be the same, this will work: <? $number[] = rand(1,100); $number[] = rand(1,100); $number[] = rand(1,100); $numbers_to_check = array(2, 16, 50); sort($number, SORT_NUMERIC); sort($numbers_to_check, SORT_NUMERIC); if($number == $numbers_to_check) { print "You found the random numbers"; } else { print "try again!"; } ?> PHP:
Even more simplified. only need 2 lines $number = array(rand(1,100), rand(1,100), rand(1,100)); echo in_array(2, $number) && in_array(16, $number) && in_array(50, $number) ? 'You found the random numbers' : 'try again!'; PHP:
Okay thank you everything seems to work pretty well but I have one more question ... What if only 2 of the numbers have to match in any order? 2,16,50 but if you have 50,2 it will say you found 2 of the numbers no matter what the other number is? These questions are more or less for me to learn how these arrays and different ways code is to be written ... thank you all for youer help NVMD I got it
Okay last question how do I make it stop if an array is true Example: $number = array(rand(1,100), rand(1,100), rand(1,100)); echo in_array(2, $number) && in_array(16, $number) && in_array(50, $number) ? 'You found the numbers 2,16,50' : 'try again!'; echo in_array(2, $number) && in_array(16, $number) ? 'You found the numbers 2,16' : 'try again!'; if it finds all three I want it to stop because now if it finds all 3 numbers then it says: You found the numbers 2,16,50Youfound the numbers 2,16
Try this. This won't matter how big either array is and everytime will only print out the numbers they actually found. $numbers = array(rand(1,100), rand(1,100), rand(1,100)); $nums_to_find = array(2, 16, 50); $nums_found = array_diff($numbers, array_diff($numbers, $nums_to_find)); echo empty($nums_found) ? "try again!" : "You found the numbers " . implode(",", $nums_found); PHP: If you need to make sure they find at least 2 or something like that, just do an if statement checking what count($nums_found) is (make sure it's >= 2 or whatever).