1

I have a switch statement in a tight loop that looks like this:

switch(true) {
    case /*expensive comparison */:
    case /*another expensive comparison */:
    case /*different expensive comparison */:
        return true;
}
return false;

I looking to see if any of the expensive comparisons is true, but only one. I know there are other ways to accomplish this, but this one looks nice and clean. The real question is: if expensive comparison evaluates to true, does it also run another expensive comparison also, or just jump to the return?

EDIT: I realize this could be accomplished with a simple or statement, but now I want to know for theory's sake.

return /*expensive comparison */
         || /*another expensive comparison */
         || /*different expensive comparison */;
Bryan Agee
  • 4,924
  • 3
  • 26
  • 42

4 Answers4

2

yes,

They are run until a break is hit. the cases below the matching one are all run regardless if they match

<?php
$i =0;
switch ($i) {
    case 0:
        echo "i equals 0";
    case 1:
        echo "i equals 1";
    case 2:
        echo "i equals 2";
}
?>

would show all 3 echos for example

Switch statements are designed to compare 1 value to many conditions. So no once it has the match it will be optimized not to look for any more.

exussum
  • 18,275
  • 8
  • 32
  • 65
  • This does not answer my question--I know that it executes statements until a break; the issue is whether it evaluates additional cases. I could not find an answer in the documentation. – Bryan Agee Oct 01 '13 at 22:27
1

PHP will execute statements from all other subsequent cases if no break is used, irrelevant of whether the cases match:

switch(1)
{
    case 1:
            echo 1;
    case 2:
            echo 2;
    case 3:
            echo 3;

    return 'done';
}

Output: 123

"...PHP continues to execute the statements until the end of the switch block, or the first time it sees a break statement. If you don't write a break statement at the end of a case's statement list, PHP will go on executing the statements of the following case."

http://php.net/manual/en/control-structures.switch.php

George
  • 81
  • 1
  • 7
  • Please see the comment on user1281385's post – Bryan Agee Oct 01 '13 at 22:27
  • OK, I believe I understand your question now. From my experience, it does not actually evaluate the other cases. Just tested this with the code included in answer – George Oct 01 '13 at 22:39
1

Your code with the switch as shown (without any breaks in it) will evaluate all of them. As far as I can recall, the || syntax will only do the comparison if the preceding evaluation is false.

On the other hand, it wouldn't take much to take a case structure and make it behave the same as the || syntax and would make for easier to read code.

Either way, the answer to this is pretty easy to acquire with just a simple test. Just echo or errorlog out something for each comparison. It is best to try and figure things out yourself before you go asking questions on a site like this.

See What have you tried? for why.

starshine531
  • 601
  • 5
  • 19
0

I finally had a chance to do some testing on this, and it looks like it (5.4 at least) stops evaluating cases once one matches. Here is how I verified that:

<?php
function evaluate($id)
{
    echo $id, "\n";
    return true;
}

switch (true) {
    case evaluate(1):
    case evaluate(2):
    case evaluate(3):
    echo "Evaluated\n";
}

Here is the output:

 >1
 >Evaluated
Bryan Agee
  • 4,924
  • 3
  • 26
  • 42