Jump to content

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)) {
    // ...
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.