2

It possible to have logical control on case ?

ie:

$v = 0;
$s = 1;

switch($v)
{
    case $s < $v:
        // Do some operation
        break;
    case $s > $v:
        // Do some other operation
        break;
}

Is there a way to do something similar ?

7
  • 1
    Why do want to use switch ? Use if-else ladder instead.
    – Riz
    Commented Oct 17, 2011 at 10:54
  • For less complex code. Is more easy to me in current code to use wsitch
    – KodeFor.Me
    Commented Oct 17, 2011 at 10:55
  • Why not try it and see for yourself?
    – Jonnix
    Commented Oct 17, 2011 at 10:56
  • I would suggest you do use if-else/elseif, not sure how it's simpler to use switch given that an if statement uses less code..
    – SW4
    Commented Oct 17, 2011 at 10:57
  • I have already test it. The problem is that I have many diferent states to test. So can I make logical control into case ?
    – KodeFor.Me
    Commented Oct 17, 2011 at 10:57

4 Answers 4

6

The condition that is passed to switch is a value the cases are compared against. The condition (in your question $v) is evaluated once and then PHP seeks for the first case that matches the result.

From the manual (after Example #2), emphasis added:

The switch statement executes line by line (actually, statement by statement). In the beginning, no code is executed. Only when a case statement is found with a value that matches the value of the switch expression does PHP begin to execute the statements. PHP continues to execute the statements until the end of the switch block, or the first time it sees a break statement.

In your question switch ($v) is same as if you'd written: switch (0), because $v = 0. Then, your switch will try to find a case which equals to 0. And, just as @Kris said:

$s < $v evaluates to false, which triggers on the $v because it is 0 in here.


If you have to use conditions in case statements, your switch-condition should be a boolean, e.g.:

$v = 0;
$s = 1;

switch (true) {
    case ($s < $v):
        echo 's is smaller than v';
    break;

    case ($s > $v):
        echo 's is bigger than v';
    break;
}

Now your switch tries to seek the first case that evaluates to true, which in this case would be $s > $v.


Note that while the switch-condition is evaluated only once, cases are each evaluated in order:

$a = 1;

switch ($a) {
    case 2:
        echo 'two';
        break;
    case (++$a > 2):
        break;
    default:
        echo $a;
}

echoes '2' from default-case, because when comparing $a to "2" $a was 1 and the case is discarded; while:

$a = 1;

switch ($a) {
    case (++$a > 2):
        break;
    case 2:
        echo 'two';
        break;

    default:
        echo $a;
}

echoes 'two' from case '2' because ++$a > 2 increases $a but doesn't match $a.

default is a fallback and its position doesn't matter.

NB: the aforementioned switches are fugly and esoteric and are only provided as a proof-of-example.

2

Not the most readable use of, but yes you can.

Use if/else - if/elseif/else structure if you do need just a simple comparison.

0
2

This will not work, every case needs to be a scalar value. in your example case it is even worse, it may seem to work but...

$s < $v evaluates to false, which triggers on the $v because it is 0 in here.

0
-2

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

if ($a > $b) {
    echo "a is bigger than b";
} elseif ($a == $b) {
    echo "a is equal to b";
} else {
    echo "a is smaller than b";
}

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