thefortrees Posted July 6, 2007 Share Posted July 6, 2007 Is it possible to compare several cases at once. Instead of having this: //Body Style $bodyStyle=$vehicle->vehicletype; switch ($bodyStyle){ case '': $bodyStyle = 'UNAVAIL'; break; case '2 Door Cab': $bodyStyle = 'TRUCK'; break; case '2 Dr Reg Cab': $bodyStyle = 'TRUCK'; break; case '4 Door Cab': $bodyStyle = 'TRUCK'; break; case '2 Door Convertible': $bodyStyle = 'CONVERTIBLE'; break; case '2 Door Coupe': $bodyStyle = 'COUPE'; break; case '2 Door Hatchback': $bodyStyle = 'HATCHBACK'; break; case '2 Door Wagon': $bodyStyle = 'WAGON'; break; case '3 Door Van': $bodyStyle = 'VAN'; break; case '4 Door Coupe': $bodyStyle = 'COUPE'; break; case '4 Door Hatchback': $bodyStyle = 'HATCHBACK'; break; case '4 Door Sedan': $bodyStyle = 'SEDAN'; break; case '4 Door Van': $bodyStyle = 'VAN'; break; case '4 Door Wagon': $bodyStyle = 'WAGON'; break; } Is there some way to simplify the statements so that the 3 cases where $bodyStyle = 'TRUCK' can be combined into 1 case? (without using an array) Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted July 6, 2007 Share Posted July 6, 2007 <?php switch($mode){ case 1: case 2: case 3: echo "1, 2, or 3"; break; case 4: default: echo "4 or anything else"; break; } ?> The break is the keyword in a switch. Switch evaluates the expression and starts execution at the corresponding case. Execution continues line by line until break is encountered, at which point execution continues after the switch is closed. Quote Link to comment Share on other sites More sharing options...
per1os Posted July 6, 2007 Share Posted July 6, 2007 //Body Style $bodyStyle=$vehicle->vehicletype; switch ($bodyStyle){ case '': $bodyStyle = 'UNAVAIL'; break; case '2 Door Cab': case '2 Dr Reg Cab': case '4 Door Cab': $bodyStyle = 'TRUCK'; break; case '2 Door Convertible': $bodyStyle = 'CONVERTIBLE'; break; case '2 Door Coupe': case '4 Door Coupe': $bodyStyle = 'COUPE'; break; case '2 Door Hatchback': case '4 Door Hatchback': $bodyStyle = 'HATCHBACK'; break; case '4 Door Wagon': case '2 Door Wagon': $bodyStyle = 'WAGON'; break; case '3 Door Van': case '4 Door Van': $bodyStyle = 'VAN'; break; case '4 Door Sedan': $bodyStyle = 'SEDAN'; break; } Quote Link to comment Share on other sites More sharing options...
effigy Posted July 6, 2007 Share Posted July 6, 2007 Or possibly... <pre> <?php $body_styles = array( '', '2 Door Cab', '2 Dr Reg Cab', '4 Door Cab', '2 Door Convertible', '2 Door Coupe', '2 Door Hatchback', '2 Door Wagon', '3 Door Van', '4 Door Coupe', '4 Door Hatchback', '4 Door Sedan', '4 Door Van', '4 Door Wagon' ); foreach ($body_styles as $style) { $name = 'UNAVAIL'; if (preg_match('/(\S+)\z/e', $style, $matches)) { $name = strtoupper($matches[1]); if ($name == 'CAB') { $name = 'TRUCK'; } } echo "$style / $name<br>"; } ?> </pre> Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted July 6, 2007 Share Posted July 6, 2007 On second thought, here is another way of doing the same thing which makes updating easier: <?php // Array of search words where 'search' => 'style' $Search = Array("cab" => "truck", "convertible" => "", "coupe" => "", "hatchback" => "", "wagon" => "", "van" => "", "sedan" => "" ); $type = $vehicle->vehicletype; // Shorthand $bodyStyle = "UNAVAIL"; // Final result // Start our search foreach($Search as $key => $val){ $pos = stripos($type, $key); // Search for $key within $type if($pos === FALSE){ continue; } // not found, skip // If we're still here, we found a match. If $val is set we // will use it, otherwise we use $key if(strlen($val)){ $bodyStyle = strtoupper($val); }else{ $bodyStyle = strtoupper($key); } } echo $bodyStyle; ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.