Home  >  Article  >  Backend Development  >  Usage and examples of case keyword in PHP

Usage and examples of case keyword in PHP

WBOY
WBOYOriginal
2023-06-28 18:39:142332browse

The case keyword in PHP is a keyword used for conditional judgment in switch statements. In programs, we often encounter situations where we need to execute different code blocks according to different conditions, and the switch statement exists to solve this problem.

The basic syntax of the switch statement is as follows:

switch (expression) {
    case label1:
        // code block 1
        break;
    case label2:
        // code block 2
        break;
    case label3:
        // code block 3
        break;
    ...
    default:
        // default code block
        break;
}

Among them, expression is an expression, and label1, label2, etc. are labels that can be used for comparison. The switch statement will check each label one by one according to the value of expression. When the value of expression is equal to a label, the corresponding code block will be executed and the switch statement will jump out. If all tags are not equal to the value of expression, the default code block will be executed.

Let’s use some examples to better understand the usage of the switch statement.

Example 1:

$day = "Monday";

switch ($day) {
    case "Monday":
        echo "今天是星期一";
        break;
    case "Tuesday":
        echo "今天是星期二";
        break;
    case "Wednesday":
        echo "今天是星期三";
        break;
    case "Thursday":
        echo "今天是星期四";
        break;
    case "Friday":
        echo "今天是星期五";
        break;
    case "Saturday":
        echo "今天是星期六";
        break;
    case "Sunday":
        echo "今天是星期日";
        break;
    default:
        echo "无效的日期";
        break;
}

In the above example, based on the value of the variable $day, we determine what day of the week today is and output the corresponding information. If the value of $day is "Monday", "Today is Monday" will be output, and so on.

Example 2:

$score = 85;

switch (true) {
    case ($score >= 90):
        echo "成绩优秀";
        break;
    case ($score >= 80):
        echo "成绩良好";
        break;
    case ($score >= 70):
        echo "成绩中等";
        break;
    case ($score >= 60):
        echo "成绩及格";
        break;
    default:
        echo "成绩不及格";
        break;
}

In the above example, we are rating students based on their scores. If the score is greater than or equal to 90, then "Excellent Score" is output, and so on.

Summary:

  • The switch statement is a statement used to execute different code blocks based on different conditions.
  • The basic syntax of the switch statement is to use expression and multiple case tags to implement conditional judgment.
  • When the value of expression is equal to a case label, the corresponding code block will be executed and the switch statement will jump out.
  • If all case labels are not equal to the value of expression, the default code block will be executed.

In actual development, the switch statement is usually used to handle multiple fixed conditional judgments, which can make the code more concise and readable. However, it should be noted that if the conditional judgment is very complex, or different code logic needs to be executed according to different situations, other control structures may need to be considered.

The above is the detailed content of Usage and examples of case keyword in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn