Jump to content

[SOLVED] Switch Operators?


papaface

Recommended Posts

I beg to differ:

<?php
$val = rand(0,30);
switch (true) {
   case ($val == 0):
       echo 'Value (' . $val . ') is zero';
       break;
   case $val > 0 && $val < 20:
       echo 'Value (' . $val . ') is between 0 and 20';
       break;
   case ($val >= 20):
       echo 'Value (' . $val . ') is greater than or equal to 20';
       break;
}
echo "<br>\n";
?>

 

Ken

I beg to differ:

<?php
$val = rand(0,30);
switch (true) {
   case ($val == 0):
       echo 'Value (' . $val . ') is zero';
       break;
   case $val > 0 && $val < 20:
       echo 'Value (' . $val . ') is between 0 and 20';
       break;
   case ($val >= 20):
       echo 'Value (' . $val . ') is greater than or equal to 20';
       break;
}
echo "<br>\n";
?>

 

Ken

Thanks. I was just reading through the switch documentation and read that it can be done using your method.

Thanks :D

I beg to differ:

<?php
$val = rand(0,30);
switch (true) {
   case ($val == 0):
       echo 'Value (' . $val . ') is zero';
       break;
   case $val > 0 && $val < 20:
       echo 'Value (' . $val . ') is between 0 and 20';
       break;
   case ($val >= 20):
       echo 'Value (' . $val . ') is greater than or equal to 20';
       break;
}
echo "<br>\n";
?>

 

Ken

 

Hmm, didn't know you could do that, but there is no reason to do that. The point of the switch, from what I see it, is that you don't have to repeat the variable name you are checking all the time.

I beg to differ:

<?php
$val = rand(0,30);
switch (true) {
   case ($val == 0):
       echo 'Value (' . $val . ') is zero';
       break;
   case $val > 0 && $val < 20:
       echo 'Value (' . $val . ') is between 0 and 20';
       break;
   case ($val >= 20):
       echo 'Value (' . $val . ') is greater than or equal to 20';
       break;
}
echo "<br>\n";
?>

 

Ken

 

Hmm, didn't know you could do that, but there is no reason to do that. The point of the switch, from what I see it, is that you don't have to repeat the variable name you are checking all the time.

Yeah, if's are better. But I asked if you can do it, not "is it better to use ifs" lol.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.