Compare a variable to mupltiple values

Discussion in 'PHP' started by archard, Aug 31, 2007.

  1. #1
    Chances are this is very simple to do but I just can't figure it out cause I'm a noob :D

    Say I have an if statement that says 'if $variable is equal to 1 then execute statement'. But what if I want to say 'if $variable is equal to 1 OR $variable is equal to 2 OR $variable is equal to 3 ... etc etc etc. All the way up to 20 lets say.

    How do I compare the variable to all these values without having to write an OR statement for every value?
     
    archard, Aug 31, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    www.php.net/in_array


    Or as you said:
    
    if ($foo == 'x' OR $foo == 'y' OR $foo == 'z' ...)
    
    PHP:
    EDIT; Forget the 2nd suggestion, I should read the whole message before replying. :D
     
    nico_swd, Aug 31, 2007 IP