switch

Tests a condition against any number of case conditions, defined within "double quotes".

 

Break must be used to terminate the switch, or the next statement will be executed.

 

A default case is utilised for when no condition is met.

 

<?php
	$myVar = rand(1,7) ;

	switch ($myVar) {
		case "1":
			echo "Random generated number was 1";
			break;
		case "2":
			echo "Random generated number was 2";
			break;
		case "3":
			echo "Random generated number was 3";
			break;
		case "4":
			echo "Random generated number was 4";
			break;
		case "5":
			echo "Random generated number was 5";
			break;
		default:
			echo "The random number greater than 5!";
	}
?>

*note that there's no case keyword before default

Leave a Reply