Home>Article>Backend Development> What is the usage of php switch?

What is the usage of php switch?

藏色散人
藏色散人 Original
2021-10-14 10:37:41 2647browse

php switch statement is used to perform different actions based on different conditions. Its usage syntax is "switch (expression){code executed when case label1:expression=label1;break...}".

What is the usage of php switch?

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

What is the usage of php switch?

PHP Switch Statement

The switch statement is used to perform different actions based on different conditions.

Switch Statement

If you want to selectively execute one of several blocks of code, use the Switch statement.

Use the Switch statement to avoid lengthy if..elseif..else code blocks.

Grammar

switch (expression) { case label1: expression = label1 时执行的代码 ; break; case label2: expression = label2 时执行的代码 ; break; default: 表达式的值不等于 label1 及 label2 时执行的代码; }

Working principle:

Perform a calculation on the expression (usually a variable)

Compare the value of the expression with the case in the structure Values are compared

If there is a match, the code associated with the case is executed

After the code is executed, the break statement prevents the code from jumping into the next case to continue execution

If not case is true, use the default statement

-Recommended learning: "PHP Video Tutorial"

Example

The above is the detailed content of What is the usage of php switch?. 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