0

Can i put conditional statement within switch statement. ex - switch ($totaltime<=13) Other than php how about other languages compatibility with it?

$totaltime=15;

switch ($totaltime<=13) {

case ($totaltime <= 1):
echo "That was fast!";
break;

case ($totaltime <= 5):
echo "Not fast!";
break;

case ($totaltime >= 10 && $totaltime<=15):
echo "That's slooooow";
break;
}

Edit

$totaltime=12; 
switch (false) { 
case ($totaltime <= 1): 
echo "That was fast!"; 
break; 
case ($totaltime <= 5): 
echo "Not fast!";
break;
case ($totaltime >= 10 && $totaltime<=13): 
echo "That's slooooow"; 
break; 
default: // do nothing break; 
} 

Gentleman in this case why alwyas show output as "That was fast!"?

7
  • what is the exact problem? Operators cannot cover all possible cases?
    – geoandri
    Commented Oct 20, 2014 at 18:58
  • 1
    This is like an almost exact duplicate of: stackoverflow.com/questions/4498739/…
    – icecub
    Commented Oct 20, 2014 at 19:00
  • @icecub but it dont have condition within switch statement. Thats why i post here that
    – Nadishan
    Commented Oct 20, 2014 at 19:04
  • You can't put a condition inside the switch parameter. Simply wrap an if statement around it: if($totaltime <= 13){ switch... } else { ...
    – icecub
    Commented Oct 20, 2014 at 19:08
  • I saw your profile. When you ask a question, please, select the right answer as accepted. You only have 2 questions with accepted answers out of 13 questions with answers. Commented Oct 20, 2014 at 19:10

3 Answers 3

3

Switch only checks if the first condition is equal to the second, this way:

switch (CONDITION) {
    case CONDITION2:
        echo "CONDITION is equal to CONDITION2";
    break;
}

So you have to do it this way:

switch (true) {
    case $totaltime <= 1: #This checks if true (first condition) is equal to $totaltime <= 1 (second condition), so if $totaltime is <= 1 (true), is the same as checking true == true.
        echo "That was fast!";
    break;

    case $totaltime <= 5:
        echo "Not fast!";
    break;

    case $totaltime >= 10 && $totaltime<=13:
        echo "That's slooooow";
    break;
}

Instead of this i'll go for if-elseif statements. Is easier to understand at first sight:

if ($totaltime <= 1) {
    echo "That was fast!";
} elseif($totaltime <= 5) {
    echo "Not fast!";
} elseif($totaltime >= 10 && $totaltime<=13) {
    echo "That's slooooow";
}
1

Yes you can (except for the comparison within switch)

$totaltime=12;

switch (true) {

case ($totaltime <= 1):
echo "That was fast!";
break;

case ($totaltime <= 5):
echo "Not fast!";
break;

case ($totaltime >= 10 && $totaltime<=13):
echo "That's slooooow";
break;

default:
// do nothing
break;
}
2
  • $totaltime=12; switch (false) { case ($totaltime <= 1): echo "That was fast!"; break; case ($totaltime <= 5): echo "Not fast!"; break; case ($totaltime >= 10 && $totaltime<=13): echo "That's slooooow"; break; default: // do nothing break; } Gentleman in this case why alwyas show out put as first case?
    – Nadishan
    Commented Oct 20, 2014 at 19:08
  • That is because you are matching to the first case statement that evaluates to false. $totaltime <= 1 evaluates to false matching the switch value and therefore executes the code within this case.
    – Barry
    Commented Oct 20, 2014 at 19:16
0

Yes you can, from the PHP's switch documentation:

The switch statement is similar to a series of IF statements on the same expression. In many occasions, you may want to compare the same variable (or expression) with many different values

When the case has constant value it's just like saying, case value == switch value, but you can have more complex expressions for a case.

Not the answer you're looking for? Browse other questions tagged or ask your own question.