php switch语句多个值匹配同一代码块的实现_PHP
先说说switch()语句的格式
switch(表达式){
case 匹配1:
当匹配1和表达式匹配成功执行的代码;
break;
case 匹配2:
当匹配2和表达式匹配成功执行的代码;
break;
default:
如果case语句没有与表达式成功所执行的代码;
}
理解 switch 是怎样执行的非常重要。switch 语句一行接一行地执行(实际上是语句接语句)。开始时没有代码被执行。仅当一个 case 语句中的值和 switch 表达式的值匹配时 PHP 才开始执行语句,直到 switch 的程序段结束或者遇到第一个 break 语句为止。如果不在 case 的语句段最后写上 break 的话,PHP 将继续执行下一个 case 中的语句段。
例子:
复制代码 代码如下:
switch($i){
case 1:
echo "$i的值是1";
break;
case 2:
echo "$i的值是2";
break;
case 3:
echo "$i的值是3";
break;
default:
echo "$i的值不是1、2、3";
}
?>
在一个 case 中的语句也可以为空,这样只不过将控制转移到了下一个 case 中的语句,知道下一个case的语句块不为空,这样就实现了多个值匹配同意代码块:
当$i的值为1或2或3时输出同一语句:
复制代码 代码如下:
switch($i){
case 1:
case 2:
case 3:
echo "$i的值为$i的值为1或2或3";
break;
}
?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide
