Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

problem with conditional switch

The example below is an extráct from http://php.net/manual/de/control-structures.switch.php

<?php
$totaltime = 0;
switch ($totaltime) {

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

    case ($totaltime > 1):
        echo "Not fast!";
        break;

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

?>

I expected the result as "That was fast." But actual result is "Not fast!". It would be great if some one can explain me why?

But if i add another case, case 0: echo "That was super fast!". Then it is echoing properly. i.e "That was super fast!". Please help me how to use conditional switch statement.

EDIT:-

Thanks all for your responses. I am able to overcome the above problem by modifyong switch($totaltime) to switch(1)

like image 673
satya Avatar asked Dec 21 '10 11:12

satya


3 Answers

case ($totaltime < 1): means 1 to PHP (that equation returns true)

case ($totaltime > 1): means 0 to PHP (that equation returns false)

Since $totaltime is 0, you get that output

In other words PHP compares $totaltime to the result of the comparisons.

EDIT regarding EDIT in OP:

You need to get rid of the switch()-statement. You only use it to easily compare against different values and not use additional expressions with it.

I mean what is wrong with

<?php
$totaltime = 0;

if ($totaltime < 1) {
    echo "That was fast!";
} else if ($totaltime > 10) {
    echo "That's slooooow";
} else if ($totaltime > 1) {
    echo "Not fast!";
}

?>

EDIT: please note that I switched the last two if-statements to make it really work.

like image 144
sjngm Avatar answered Sep 18 '22 12:09

sjngm


You don't use conditionals in the case statements like that, not intuitively anyway. This is what's happening:

    case ($totaltime < 1):  // Evaluates to 1.  $totaltime is not 1, so no match.
    case ($totaltime > 1):  // Evaluates to 0.  $totaltime is 0, so match.

Essentially you're trying to use an else if construct as a switch construct, but the functionality isn't there. The conditionals don't evaluate in the way you're expecting (the way they would in an if block), they're just looking for the first case block which equals the value being tested in the switch block.

like image 24
David Avatar answered Sep 18 '22 12:09

David


Hate to necro a post that's already answered but I am rather baffled no one touched on the switch(true) method.

There is no real world speed advantage of either method

In some cases the switch was faster, others the if was faster, but only by fractions of a microsecond (48.16 µs vs 49.11 µs switch faster than if).

EDIT

And now I see the OP did the same...

<?php
for ( $totaltime = 0; $totaltime < 11; $totaltime += 0.5 ) {
    switch ( true ) {
        case ( $totaltime < 1 ):
            echo $totaltime . " That was fast!\n";
            break;
        case ( $totaltime < 10 ):
            echo $totaltime . " Not fast!\n";
            break;
        default:
            echo $totaltime . " That's slooooow\n";
            break;
    }
}

Results: https://3v4l.org/d71lZ

0 That was fast!
0.5 That was fast!
1 Not fast!
1.5 Not fast!
2 Not fast!
2.5 Not fast!
3 Not fast!
3.5 Not fast!
4 Not fast!
4.5 Not fast!
5 Not fast!
5.5 Not fast!
6 Not fast!
6.5 Not fast!
7 Not fast!
7.5 Not fast!
8 Not fast!
8.5 Not fast!
9 Not fast!
9.5 Not fast!
10 That's slooooow
10.5 That's slooooow
like image 26
Will B. Avatar answered Sep 21 '22 12:09

Will B.