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 ;)