0
$Variable = "Dog"

if($Variable == "Cat"){
     do stuff
}
elseif($Variable == "Goat"){
     do other stuff
}
elseif($Variable == "Cash"){
     Run some other stuff
}

How would I write code to say echo "hi" if and only if one of the statements were true? My Question is do I have to write echo "hi" inside each statement? or can I save lines by doing it a certain way?

  • 2
    1) You know that `=` is an assignment and not comparison?! 2) Either write a if statement combining all conditions by an OR operator or put it into every if statement. – Rizier123 Jun 13 '16 at 19:13
  • The dupe is for JS, but holds the same for PHP. – Marc B Jun 13 '16 at 19:14
  • 1
    @MarcB The dupe is for PHP ;), it just got tagged with JS probably since it is pretty much the same. I think OP just wants to output something when one of the 3 conditions he has are true (But the question body and title are a bit controversial). – Rizier123 Jun 13 '16 at 19:16
  • Fixed the =? Yeah @Rizier123 hit it right on the nail sorry I wasn't sure how to ask the question! – CodingMageSheen Jun 13 '16 at 19:19
  • Switch isn't exactly what I'm looking for. I'm just asking is the only possible/shortest way to do this is add the echo "hi" inside every statement? I only want the thing to run if ONE of the conditions are met. – CodingMageSheen Jun 13 '16 at 19:21
  • That's a weird duplicate @MarcB... – giorgio Jun 13 '16 at 19:24
  • I only changed "=" to "==" I didn't really change the question. @chris85 okay the thing is I have a function called save() I want that function to run only if one of them is true. Inside the switch case I would still have to write save() inside each one right? – CodingMageSheen Jun 13 '16 at 19:27
  • 1
    @CodingMageSheen As I said either write an if statement that combines all conditions with an OR operator or put it into each if body. – Rizier123 Jun 13 '16 at 19:28
  • 1
    You can write multiple conditions if you want to perform the same code. `if ($animal == "cat" || $animal == "dog") { save(); }` will do it if the animal is a dog or a cat. – Qirel Jun 13 '16 at 19:29
  • Oh, you want to output `hi` if any condition is met. Yea, just separate conditions in one `if` with `||`. – chris85 Jun 13 '16 at 19:31
  • Okay awesome thanks for the input everyone I'll thumbs up! I just was curious if there was a simple way to do this that I didn't know about. I like learning all the tricks if possible! – CodingMageSheen Jun 13 '16 at 19:31
  • I cant thumbs up comments so if you provide an answer I'll upvote! – CodingMageSheen Jun 13 '16 at 19:32
  • @Qirel You wrote complete answer, might as well move it down. – chris85 Jun 13 '16 at 19:35

1 Answers1

2

There isn't a special control structure for this, but there are several ways of achieving this without having to write echo 'hi'; three times. It's partly a matter of taste, and partly a matter of what the real situation is. For example if you're just saying 'hi', it all doesn't really matter, but if you want to do something complex, it's an other story. A few suggestions:

1. Write another if/else clause

if ( $variable == "Cat" || $variable == "Dog" || $variable == "Goat" ) {
    echo 'hi!';
}

2. Use the else to exclude

$say_hi = true;

if( $Variable == "Cat" ){
    // do stuff
} else if( $Variable == "Dog" ){
    // do other stuff
} else if( $Variable == "Goat" ){
    // do whatherever
} else {
    $say_hi = false;
}

if ( $say_hi ) {
    echo 'hi';
}

3. Use a function

This one really depends on your use case, but it could be feaseable.

function feed( $animal ) {
    if ( $animal == 'cat' ) {
        // feed the cat
        return true;
    } else if ( $animal == 'dog' ) {
        // feed the dog;
        return true;
    } else if ( $animal == 'goat' ) {
        // feed the goat
        return true;
    }
    return false;
}

if ( feed('dog') ) {
    echo 'hi';
}

if ( feed('cat') ) {
    echo 'hi again';
}

4. Use arrays

This one also depends on your use case, but can be handy as well

function cat_function() {
    echo 'The cat says meaauw';
}

function dog_function() {
    // etc
}

function goat_function() {
    // you got the point
}

$animals = array(
    'cat'  => 'cat_function', 
    'dog'  => 'dog_function', 
    'goat' => 'goat_function'
);

$my_pet = 'dog';

if ( array_key_exists( $my_pet, $animals ) ) {
    call_user_func( $animals[ $my_pet ] );
}

Ok I can think of some others, but I'd need your use case ;)

giorgio
  • 10,111
  • 2
  • 28
  • 41