Syntax: switch (testvariable) { caselist; }


Switch is a unique sort of loop that is often used in place of very long if cascades. Possible values of testvariable are enumerated in the case list, so that if testvariable has the value listed as a case, the code following that case statement, up to the first break, is executed.

If no break is found between one case statement and the next, then the following case statement is also executed. This is called case fall-through, and there is controversy surrounding whether or not to use it.

The default statement is usually the last statement in a switch case list. It is executed if no other value in the list is matched, or if the preceding case statement is executed and lacks a break.


if (myInteger <= 10) {
  switch (myInteger) {
	case 1:
	  echo("my integer's value is one");
	case 2:
	case 3:
	case 5:
	case 7:
	  echo("my integer's value is prime!");
	  echo("my integer's value is boring. :(");



There are 2 comments on this page. [Display comments]

Valid XHTML 1.0 Transitional :: Valid CSS :: Powered by WikkaWiki