Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make using "alternative syntax" rather than "curly-brace syntax" in php? (Using Endswitch Syntactic Sugar)

Tags:

syntax

php

This is my php code:

switch ($i) {
    case 0:
        echo '$i is 0.';
        break;
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
        echo '$i is somewhere between 1 and 5.';
        break;
    case 6:
    case 7:
        echo '$i is either 6 or 7.';
    default:
        echo "I don't know how much \$i is.";
}
?>

Now, how can I make the code using alternative syntax rather than curly-brace syntax?

like image 815
Hamed Azimi Avatar asked Feb 19 '14 18:02

Hamed Azimi


2 Answers

In this case i dont even feel there is a requirement of using Switch, better go with if or if else statement.

if($i==0){
  echo "value is 0";
} else if($i>0 && $i<6){
  echo "value is between 1 and 5";
}else if($i>5 && $i<8){
  echo "value is 6 or 7";
}else{
  echo "unknown value";
}
like image 130
Akhil Sidharth Avatar answered Oct 24 '22 11:10

Akhil Sidharth


The actual answer to your question if you want to use alternative syntax with this example is:

switch ($i):
        case 0:
            echo '$i is 0.';
            break;
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
            echo '$i is somewhere between 1 and 5.';
            break;
        case 6:
        case 7:
            echo '$i is either 6 or 7.';
        default:
            echo "I don't know how much \$i is.";
    endswitch;
    ?>
like image 2
Alex P Avatar answered Oct 24 '22 11:10

Alex P