Switch Assessment

I recently got an argument about how it switchhandles comparisons and needs help to determine it.

If I write a switch, for example:

switch ($x){
    case ($x > 5):
        echo "foo";
        break;
    case ($x < 5):
        echo "bar";
        break;
    default:
        echo "five";
        break;
}

Which operator is ifequivalent? A or B ?

// A

if ($x > 5) {
    echo "foo";
} elseif ($x < 5) {
    echo "bar";
} else {
    echo "five";
}

// B

if ($x == ($x > 5)) {
    echo "foo";
} elseif ($x == ($x < 5)) {
    echo "bar";
} else {
    echo "five";
}
0
source share
4 answers

Everyone, let me clarify:

It is equivalent B.

It’s not “both,” it’s not sometimes one thing, sometimes it’s another, it’s always B. To understand why you sometimes see results indicating that it might be A, you need to understand how type coercion works in PHP .

false "" switch case, , FALSE.


switch if/elseif, (== ===) , switch ( ), case ( ).

:

$x = 0;

switch ($x) {
  case $x > -1: // This is TRUE, but 0 == FALSE so this won't match
    echo "case 1";
  case $x == -1: // This is FALSE, and 0 == FALSE so this will match
    echo "case 2";
}

if/elseif:

A:

$x = 0;

if ($x > -1) {
  // It matches here
  echo "case 1";
} else if ($x == -1) {
  // and not here
  echo "case 2";
}

B:

$x = 0;

if ($x == ($x > -1)) {
  // It doesn't match here
  echo "case 1";
} else if ($x == ($x == -1)) {
  // ..but here instead
  echo "case 2";
}
+5

B

:

, , $x 0:

$x > 5 false ( 0 false), , , foo.        :

switch ($x){
    case false:
        echo "foo";
        break;
    case true:
        echo "bar";
        break;
    default:
        echo "five";
        break;
}

foo ( , B)

2:

, :

$x = 0 = > (foo), A (bar), B (foo)

$x = 5 = > (), A (), B ()

$x = 7 = > (foo), A (foo), B (foo)

+2

B.

( A), :

switch (true) { // Use true instead of $x
    case ($x > 5):
        echo "foo";
        break;
    case ($x < 5):
        echo "bar";
        break;
    default:
        echo "five";
        break;
}
+2

" - B", SO , " 30 , 15".

, PHP 5.4.0:

<?php

<?php

function test($x) {
    echo "Switch: ";

    switch ($x){
        case ($x > 5):
            echo "foo";
            break;
        case ($x < 5):
            echo "bar";
            break;
        default:
            echo "five";
            break;
    }

    echo "\nA: ";

    if ($x > 5) {
        echo "foo";
    } elseif ($x < 5) {
        echo "bar";
    } else {
        echo "five";
    }

    echo "\nB: ";

    // B

    if ($x == ($x > 5)) {
        echo "foo";
    } elseif ($x == ($x < 5)) {
        echo "bar";
    } else {
        echo "five";
    }
}

echo "Test with 6:\n\n";
test(6);
echo "\n\nTest with 4:\n\n";
test(4);
echo "\n\nTest with 5:\n\n";
test(5);
echo "\n\nTest with true:\n\n";
test(true);
echo "\n\nTest with false:\n\n";
test(false);
echo "\n\n";

:

    Test with 6:

Switch: foo
A: foo
B: foo

Test with 4:

Switch: bar
A: bar
B: bar

Test with 5:

Switch: five
A: five
B: five

Test with true:

Switch: five
A: five
B: five

Test with false:

Switch: foo
A: bar
B: foo
0
source

All Articles