Leilin Peng Share: PHP Switch Statement

  switch statement is used to perform different actions according to a plurality of different conditions.

  PHP Switch Statement

  If you want to selectively execute one of several blocks of code, use a switch statement.

  

  switch (n)

  {

  case label1:

  If n = label1, where the code execution;

  break;

  case label2:

  If n = label2, where the code execution;

  break;

  default:

  If n is equal to neither equal nor label1 label2, where the code execution;

  }

  ?>

  How it works: First, a simple expression n (usually variable) once calculated. The value of the structure of an expression value of each case is compared. If there is a match, associated with the case code is executed. After the code is executed, the code used to prevent break jumped next case to continue. Default statement is executed when a match does not exist (i.e., no case is true).

  

  $favcolor="red";

  switch ($favcolor)

  {

  case "red":

  echo "Your favorite color is red!";

  break;

  case "blue":

  echo "Your favorite color is blue!";

  break;

  case "green":

  echo "Your favorite color is green!";

  break;

  default:

  echo "Your favorite color is not red, blue, or green!";

  }

  ?>

  View all PHP tutorial article: https://www.codercto.com/courses/l/5.html (edit: Leilin Peng Source: network intrusion deleted)

Guess you like

Origin www.cnblogs.com/linpeng1/p/10979051.html