Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use case in php

Shulou Source: shulou.com Published: 2022-06-02 04:29:55 09月21日 Update

This article will explain in detail how to use case in php. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Case in php is used in switch statements, which uses syntax such as "switch (n) {case label1:break;case label2:break;}", which compares the value of the expression with the value of each case in the structure, and then performs different actions on a conditional basis.

This article operating environment: Windows7 system, PHP7.1 version, Dell G3 computer.

How to use case in php

PHP Switch statement

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

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

Grammar

How it works: first evaluate a simple expression n (usually a variable). Compare the value of the expression with the value of each case in the structure. If there is a match, the code associated with the case is executed. After the code is executed, use break to prevent the code from jumping into the next case to continue execution. The default statement is used to execute when no match exists (that is, no case is true).

Example

Output result:

Your favorite color is red!

This is the end of the article on "how to use case in php". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Statement code different article expression grammar action more condition structure good practical content principle variable multiple instance article is in the environment Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Shulou Information Microsoft Shulou Tech Info vpn