I've got some code using a lot of complex if statements that I'm using for a VIN decoder. I finally got it working and I'm hesitant to alter it but these if statements are huge! If a switch statement could be used to accomplish what the if statements are doing, could someone give me an example using my data? Here's a snippet of code:

$ModelYear=substr($decode,5,1);
$AssemblyPlant=strtoupper(substr($decode,6,1));
          if ($AssemblyPlant == "2" && ($ModelYear >= 5 && $ModelYear <= 7)){
		echo "$AssemblyPlant = St. Therese, Canada<br />";
	} elseif ($AssemblyPlant == "7" && $ModelYear == "9"){
		echo "$AssemblyPlant = Lordstown, OH<br />";
	} elseif ($AssemblyPlant == "B" && $ModelYear == "7"){
		echo "$AssemblyPlant = Baltimore, MD<br />";
	} elseif ($AssemblyPlant == "C"){
		echo "$AssemblyPlant = Southgate, California<br />";
	} elseif ($AssemblyPlant == "E" && ($ModelYear >= 6 && $ModelYear <= 9)){
		echo "$AssemblyPlant = Linden, NJ<br />";
        } elseif ($AssemblyPlant == "G"){
		echo "$AssemblyPlant = Framingham, Massachusetts<br />";
        } elseif ($AssemblyPlant == "H"){
		echo "$AssemblyPlant = Flint, Michigan<br />";
	} elseif ($AssemblyPlant == "K" && ($ModelYear >= 5 && $ModelYear <= 7)){
		echo "$AssemblyPlant = Leeds, MO<br />";
	} elseif ($AssemblyPlant == "L" && ($ModelYear >= 3 && $ModelYear <= 7)){
		echo "$AssemblyPlant = Van Nuys, CA<br />";
	} elseif ($AssemblyPlant == "N" && $ModelYear == "4"){
		echo "$AssemblyPlant = Norwood, OH<br />";
	} elseif ($AssemblyPlant == "T" && ($ModelYear >= 6 && $ModelYear <= 9)){
		echo "$AssemblyPlant = Tarrytown, NY<br />";
	} elseif ($AssemblyPlant == "U" && $ModelYear == "8"){
		echo "$AssemblyPlant = Lordstown, OH<br />";
	} elseif ($AssemblyPlant == "W" && ($ModelYear >= 8 && $ModelYear <= 9)){
		echo "$AssemblyPlant = Willow Run, MI<br />";
        } elseif ($AssemblyPlant == "X"){
		echo "$AssemblyPlant = Fairfax, Kansas<br />";
        } elseif ($AssemblyPlant == "Y" && ($ModelYear >= 2 && $ModelYear <= 5)){
		echo "$AssemblyPlant = Wilmington, Delaware<br />";
        } elseif ($AssemblyPlant == "Z"){
		echo "$AssemblyPlant = Fremont, California<br />";
} else {
		echo "Buick's weren't built at this plant in 197$ModelYear. Please check your VIN and try again.<br />";
}

Thanks!

Hy, As i read your description I think if you are using switch statements the coding line should be same no difference only thing is that code looking is good but not code line

Thanks

Hi. I don't think a switch statement will make your code better or easier to read.

In my (humble) opoinion you're better off creating a lookup table containing: plant, modelyearfrom, modelyearto, description.

You can use a switch statament, but you must also use "default" case

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.