is there such thing (in PHP anyway) for an OR operator in a switch case?
something like..
switch ($value) {
case 1 || 2:
echo 'the value is either 1 or 2';
break;
}
is there such thing (in PHP anyway) for an OR operator in a switch case?
something like..
switch ($value) {
case 1 || 2:
echo 'the value is either 1 or 2';
break;
}
switch ($value)
{
case 1:
case 2:
echo "the value is either 1 or 2.";
break;
}
This is called "falling though" the case block and it exists in most languages which implement a switch statement.
Try
switch($value) {
case 1:
case 2:
echo "the value is either 1 or 2";
break;
}
ah i see, so as long as i order them in the right fashion I can achieve this? excellent, and once again thank you!
I won't repost the other answers because they're all correct, but I'll just add that you can't use switch for more "complicated" statements, eg: to test if a value is "greater than 3", "between 4 and 6", etc. If you need to do something like that, stick to using if
statements, or if there's a particularly strong need for switch
then it's possible to use it back to front:
switch (true) {
case ($value > 3) :
// value is greater than 3
break;
case ($value >= 4 && $value <= 6) :
// value is between 4 and 6
break;
}
but as I said, I'd personally use an if
statement there.