-3

Possible Duplicate:
php if statement with multiple conditions

$style = get_option('background_style');
if ($style == "option1") {
    echo '<div class="background-style1">';
} 

This is what I've got if I want "option 1" to echo the div class .background-style1, but I'm wondering how I could list more options after "option 1".

I've tried this:

if ($style == "option1", "option2", "option3")

But it doesn't work. What is the right way of doing this?

Community
  • 1
  • 1
user1707297
  • 37
  • 2
  • 5

5 Answers5

4
if (in_array($style, array("option1", "option2", "option3")) {
Nemoden
  • 8,816
  • 6
  • 41
  • 65
2
if ($style == 'option1' or $style == 'option2' or $style == 'option3') {
    // do something...
}

This is the most basic way to include multiple conditions in an if statement, you can also use other logical operators. for more info see here:

http://php.net/manual/en/language.operators.logical.php

Dan
  • 3,750
  • 5
  • 24
  • 38
1

Depends on your actual requirement.

If you want the same div to be displayed for option 1, 2 and 3 :

 if ($style == "option1" || $style == "option2" || $style == "option3")

If you want to address each condition separate, go for a switch statement as mentioned by @Freethinker.

janenz00
  • 3,315
  • 5
  • 28
  • 37
0

What you want is called a switch statement.

switch($style)
{
    case 'option1':
    // code for option 1;
    break;
    case 'option2':
    // code for option 2;
    break;
    // etc...
}
Matt Whitehead
  • 1,743
  • 3
  • 19
  • 34
0
if (preg_match('/^option[1-3]$/', $style)) {
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Hello @Barmar sir, can you help me out in this issue https://stackoverflow.com/questions/51834568/how-to-call-json-from-multiple-nested-if-condition – user9437856 Aug 14 '18 at 08:44