Supid PHP Tricks: Broken Switch

A fragment of PHP that really shouldn’t work. I hope nobody ever finds a use for this:

switch($n)
{
    case 1;
        echo 'one';
        break;
    case 2;
        echo 'two';
        break;
    case 3;
        echo 'three';
        break;
}

Tags:

  1. Willem Stuursma’s avatar

    That’s weird, it works exactly the same with the semicolon as with a double colon.

  2. Simple test’s avatar

    And this one?

    switch(true) {
    case($n==(int) 1);
    echo ‘one’;
    break;
    case ($n==2);
    echo ‘two’;
    break;
    case ($n==3);
    echo ‘three’;
    break;
    }