Differences between switch and if/else statements

How it looks with switch statements:
<?php
	$number = 1;
	
	switch ($number) {
		case 1:
			echo 'One';
		break;
		
		case 2:
			echo 'Two';
		break;
		
		case 3:
			echo 'Three';
		break;
		
		default:
			echo 'Not a number between 1-3';
		break;
	}
?>
How it looks with if/else statements:
<?php
	$number = 1;
	
	if ($number==1) {
		
		echo 'One';
			
	  } else if ($number==2) {
	  	
		echo 'Two';
		
	  } else if ($number==3) {
	  
	  	echo 'Three';
	  	
	  } else {
	  
	  	echo 'Not a number between 1-3';
	}
?>
Whichever one you choose is completely preference.

Go back