Switch() multiple cases

hi all

Is it possible to convert the below code to switch statement.


<?
if(($category == 20) && ($dealer == 25))
	{
	$qry="select * from brands where gender='men'";
	}
	
if(($category == 20) && ($dealer == 26))
	{
	$qry="select * from brands where gender='women'";
	}	
?>

vineet

You could do this


switch (true) {
  case (($category == 20) && ($dealer == 25)): 
    $qry="select * from brands where gender='men'"; 
    break;
  case (($category == 20) && ($dealer == 26)): 
    $qry="select * from brands where gender='women'"; 
    break;
} 

But I don’t know if it is more readable and understandable?

Are there potentially many more of these conditions to cater for? If so, how many and what do some of them look like?

thanks guido

i didnt knew it was possible.

I was only having knowledge about the standard switch statement with 1 case

vineet

hi cups

at present there are only 4 similar cases.

vineet


switch (true) {
  case (($category == 20) && ($dealer == 25)): 
    $where = " gender ='men'";
    break;
  case (($category == 20) && ($dealer == 26)): 
    $where = "gender = 'women'"; 
    break;
}

$query = "select * from brands where " . $where; 

Does it have to be switch ?


$where = array(
   20 =&gt; array(
      25 =&gt; 'gender="men"',
      26 =&gt; 'gender="women"',
   )
);

if (isset($where[$category][$dealer]))
{
    $query = "select * from brands where ".$where[$category][$dealer];
}
else
{
    throw new \\Exception("Unknown category/dealer combination (`$category`, `$dealer`)");
}