Problem with conditional switch

Below is an example 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 slooooow"; break; } ?> 

I was expecting a result like "It was fast." But the actual result is "Not Fast!" It would be great if someone could explain to me why?

But if I add another case, case 0: echo "That was super fast!". Then it will echo correctly. ie "It was very fast!". Please help me use the conditional switch statement.

EDIT: -

Thank you all for your answers. I can overcome this problem with the modifyong ($ totaltime) switch to switch (1)

+6
php switch-statement
source share
6 answers

case ($totaltime < 1): means 1 for PHP (this equation returns true)

case ($totaltime > 1): means 0 for PHP (this equation returns false)

Since $totaltime is 0, you get this output

In other words, PHP compares $totaltime with the result of the comparison.

EDIT EDIT IN OP:

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

I mean, what's wrong with

 <?php $totaltime = 0; if ($totaltime < 1) { echo "That was fast!"; } else if ($totaltime > 10) { echo "That slooooow"; } else if ($totaltime > 1) { echo "Not fast!"; } ?> 

EDIT: note that I switched the last two if statements so that it really works.

+10
source share

You do not use conditional expressions in case like this, but not intuitively. This is what happens:

  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 are trying to use the else if construct as a switch construct, but there is no functionality there. Conditional values ​​are not evaluated as you expect (as they would be in the if block), they just look for the first case block, which is equal to the one tested in the switch block.

+2
source share

Hate for a necro message that already answered, but I'm rather puzzled, no one has touched the switching method (the truth).

There is no real speed advantage in the world of any method

In some cases, the switch was faster, others - if, but faster, but only a fraction of microseconds (48.16 ΞΌs versus 49.11 ΞΌs faster than if).

EDIT

And now I see that 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 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 slooooow 10.5 That slooooow 
+2
source share

It would seem that this is a problem of redeployment.

The first case statement will evaluate anything but 0 so that it doesn't fall.

But the second case statement will evaluate to false, which should be 0, which is equal to the one you set $ totaltime to.

+1
source share

Lucky

The PHP switch is similar to some IF statements. Cases are rated as:

 if($totaltime == ($totaltime < 1)) { echo "That was fast!"; break; } if($totaltime == ($totaltime > 1)) { echo "Not fast!"; break; } ... 

It is clear that 0 == false for the second IF is true and therefore the result.

Thanks, Vikas.

+1
source share
Others talked about why this was happening (abuse of conditional in the statement of case), but they did not offer an alternative. The switch is designed to cover certain arguments, such as {0, 1, 2, 3..100, 101}, etc. It separates specific arguments or ranges, rather than performing a simple if / else (as you used it). You can rewrite your arguments to disable it:
 switch ($totaltime) { case (0): echo "That was fast!"; break; case (1..PHP_INT_MAX): echo "Not fast!"; break; default: echo "That slooooow"; break; } 

Here .. allows you to cover the range, so everything from 1 to integer max is covered by this case. 0 is processed explicitly, and all others (re: <0) are covered by default.

0
source share

All Articles