Jump to content

Quick easy question on syntax


Jeffro

Recommended Posts

I can't seem to find this with a Google search because it's hard to find relevant info on a search where the primary topic is "or"... so I apologize for what is probably a pretty simple and easy question, but here goes...

 

Is there a really easy way to code,

 

If ($variable = "condition1" or "condition2" or "condition3" or "condition4" or "condition5") {
do this;
}

....or does this have to be written out as an If else? 

 

Thanks. 

Link to comment
https://forums.phpfreaks.com/topic/239132-quick-easy-question-on-syntax/
Share on other sites

Well, first off you'd need to use the comparison operator, == (not =).

 

Unfortunately you can't do exactly what you're proposing in PHP, you would have to do:

 

if($var == 'something' || $var == 'something else' || ..)

 

If you have a lot of possible values, alternatively, you could put them in an array and use in_array:

 

$possible_values = array('something', 'something else', 'something else else');
if(in_array($var, $possible_values)) {
    // ...
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.