3 PHP - PHP Fundamentals - PHP Tutorials

PHP Fundamentals

switch ... case

Switch...case is an alternative to if...else. Switch...case can only be used if all conditions are based around one variable. This method the variable is declared once at the begining and then conditions are called.

<? 
switch ($sport) {
  case 
"1":
  echo 
"Basketball";
  break;
  case 
"2":
  echo 
"Baseball";
  break;
  case 
"3":
  echo 
"Football";
  break;
  case 
"4":
  echo 
"Hockey";
  break;
  case 
"5":
  echo 
"Golf";
  break;
  case 
"6":
  echo 
"Soccer";
  break;
  default:
  echo 
"None";
  break;
}
 
?>

Switch starts the process. Each condition is represented by case and ended with a break. Notice case ends with a : not a semicolon. Default is used if none of the cases matches the variable.

Math Functions and Flow Control <<  1 2 3 4 5 6  >> Loops
New Content