Jump to content

simplify switch


thefortrees

Recommended Posts

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)

 

 

Link to comment
https://forums.phpfreaks.com/topic/58749-simplify-switch/
Share on other sites

<?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.

Link to comment
https://forums.phpfreaks.com/topic/58749-simplify-switch/#findComment-291431
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/58749-simplify-switch/#findComment-291433
Share on other sites

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>

Link to comment
https://forums.phpfreaks.com/topic/58749-simplify-switch/#findComment-291441
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/58749-simplify-switch/#findComment-291453
Share on other sites

Archived

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

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