php switch case

Syntax with "endswitch" used mostly in complex templates where easier to find "endforeach" than "}":

<?php
switch( $i ) :
	case 0:
		echo 'i = 0';
		break;
	case 1:
	case 11:
		echo 'i = 1';
		break;
	default:
		echo 'i != 0 and i != 1';
endswitch;
?>

Classic syntax:

<?php
switch( $i ) {
	case 0:
		echo 'i = 0';
		break;
	case 1:
		echo 'i = 1';
		break;
	default:
		echo 'i != 0 and i != 1';
}
?>

Alternative switch (with semicolon instead of a colon) syntax for combining cases:

<?php
switch( $i ) :
	case 0;
	case 1;
		echo 'i = 0 or i = 1';
	break;
	default;
		echo 'i != 0 and i != 1';
	break;
endswitch;
?>

Leave a Comment