Jump to content

Archived

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

SharkBait

Switch()

Recommended Posts


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

[code]
<?php
$value = 192;

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

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

Share this post


Link to post
Share on other sites
[code=php:0]
<?php
$value = 192;
if($value >= 1 && $value <= 400)
{
  // Do something
}
elseif($value >= 401 && $value <= 601)
{
  // Do something else
}
?>
[/code]

Share this post


Link to post
Share on other sites
[code]

<?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;
}

?>

[/code]

Share this post


Link to post
Share on other sites
Ah both of those make sense. I forgot I could do comparisons in the cases.
Thanks :)

Share this post


Link to post
Share on other sites

×

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.