1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

If then and or?

Discussion in 'PHP' started by mokimofiki, Jan 2, 2009.

  1. #1
    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 :)
     
    mokimofiki, Jan 2, 2009 IP
  2. e96

    e96 Active Member

    Messages:
    299
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    80
    #2
    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.
     
    e96, Jan 2, 2009 IP
  3. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #3
    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.
     
    bartolay13, Jan 2, 2009 IP
  4. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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:
     
    zerxer, Jan 2, 2009 IP
  5. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #5
    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:
     
    Kaizoku, Jan 2, 2009 IP
    mokimofiki likes this.
  6. mokimofiki

    mokimofiki Well-Known Member

    Messages:
    444
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    130
    #6
    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
     
    mokimofiki, Jan 5, 2009 IP
  7. mokimofiki

    mokimofiki Well-Known Member

    Messages:
    444
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    130
    #7
    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
     
    mokimofiki, Jan 5, 2009 IP
  8. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #8
    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).
     
    zerxer, Jan 5, 2009 IP