Jump to content

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.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.