Jump to content


Photo

Switch()


  • Please log in to reply
3 replies to this topic

#1 SharkBait

SharkBait
  • Members
  • PipPipPip
  • Advanced Member
  • 845 posts
  • LocationMetro Vancouver, BC

Posted 05 October 2006 - 07:51 PM


If I need toa  large range of values in a switch what is the best way?

<?php
$value = 192;

switch($value) {
   case 1 to 400:
      // Do something
      break;
    case 401 to 601:
      // Do something else
       break;
}
?>

Its similar to VB.NET with 1 to 400, not sure if PHP can do the same thing.

#2 brendandonhue

brendandonhue
  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts

Posted 05 October 2006 - 08:14 PM

<?php
$value = 192;
if($value >= 1 && $value <= 400)
{
  // Do something
} 
elseif($value >= 401 && $value <= 601)
{
  // Do something else
}
?>


#3 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 05 October 2006 - 09:25 PM


<?php

$value = 192;

switch(true)
{
case in_array($value, range(1,400)): echo "between 1-400"; break;
case in_array($value, range(401,601)): echo "between 401-601"; break;
}

?>



#4 SharkBait

SharkBait
  • Members
  • PipPipPip
  • Advanced Member
  • 845 posts
  • LocationMetro Vancouver, BC

Posted 05 October 2006 - 10:35 PM

Ah both of those make sense. I forgot I could do comparisons in the cases.
Thanks :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users