PHP的switch判断语句的“高级”用法详解,switch详解
PHP的switch判断语句的“高级”用法详解,switch详解
只所以称为“高级”用法,是因为我连switch的最基础的用法都还没有掌握,so,接下来讲的其实还是它的基础用法!
switch 语句和具有同样表达式的一系列的 IF 语句相似。很多场合下需要把同一个变量(或表达式)与很多不同的值比较,并根据它等于哪个值来执行不同的代码。这正是 switch 语句的用途。
注意: 注意和其它语言不同,continue 语句作用到 switch 上的作用类似于 break。如果在循环中有一个 switch 并希望 continue 到外层循环中的下一个轮回,用 continue 2。
下面两个例子使用两种不同方法实现同样的事,一个用一系列的 if 语句,另一个用 switch 语句:
Example #1 switch 结构
复制代码 代码如下:
if ($i == 0)
{
echo "i equals 0";
}
elseif ($i == 1)
{
echo "i equals 1";
}
elseif ($i == 2)
{
echo "i equals 2";
}
switch ($i)
{
case 0:
echo "i equals 0";
break;
case 1:
echo "i equals 1";
break;
case 2:
echo "i equals 2";
break;
}
?>
Example #2 switch 结构可以用字符串
复制代码 代码如下:
switch ($i)
{
case "apple":
echo "i is apple";
break;
case "bar":
echo "i is bar";
break;
case "cake":
echo "i is cake";
break;
}
?>
重点:(这就是我先前一直没掌握的地方!)
为避免错误,理解 switch 是怎样执行的非常重要。switch 语句一行接一行地执行(实际上是语句接语句)。开始时没有代码被执行。仅当一个 case 语句中的值和 switch 表达式的值匹配时 PHP 才开始执行语句,直到 switch 的程序段结束(如 return 语句)或者遇到第一个 break 语句为止。如果不在 case 的语句段最后写上 break 的话,PHP 将继续执行下一个 case 中的语句段。例如:
复制代码 代码如下:
switch ($i)
{
case 0:
echo "i equals 0";
case 1:
echo "i equals 1";
case 2:
echo "i equals 2";
}
?>
特别说明:这里如果 $i 等于 3,PHP不会执行任何echo语句!但是,如果 $i 等于 0,PHP 将执行所有的 echo 语句!如果 $i 等于 1,PHP 将执行后面两条 echo 语句。只有当 $i 等于 2 时,才会得到“预期”的结果——只显示“i equals 2”。所以,别忘了 break 语句就很重要(即使在某些情况下故意想避免提供它们时)。
[效率]在 switch 语句中条件只求值一次并用来和每个 case 语句比较。在 elseif 语句中条件会再次求值。如果条件比一个简单的比较要复杂得多或者在一个很多次的循环中,那么用 switch 语句可能会快一些。
在一个 case 中的语句也可以为空,这样只不过将控制转移到了下一个 case 中的语句。
复制代码 代码如下:
switch ($i)
{
case 0:
case 1:
case 2:
echo "i is less than 3 but not negative";
break;
case 3:
echo "i is 3";
}
?>
一个 case 的特例是 default。它匹配了任何和其它 case 都不匹配的情况。例如:
复制代码 代码如下:
switch ($i)
{
case 0:
echo "i equals 0";
break;
case 1:
echo "i equals 1";
break;
case 2:
echo "i equals 2";
break;
default:
echo "i is not equal to 0, 1 or 2";
}
?>
case 表达式可以是任何求值为简单类型的表达式,即整型或浮点数以及字符串。不能用数组或对象,除非它们被解除引用成为简单类型。
【实战】 根据上面的知识点,编写这么一个函数:计算容量值实际代表的字节数
复制代码 代码如下:
/**
* 返回字节数
*
* @param string $val 如 400M
*/
function return_bytes($val = '')
{
$val = trim($val);
$last = strtolower($val{strlen($val)-1});
switch ($last)
{
case 'g':
$val *= 1024;
case 'm':
$val *= 1024;
case 'k':
$val *= 1024;
}
return $val;
}
$memorylimit = ini_get('memory_limit');
echo $memorylimit, '
';
echo return_bytes($memorylimit);
输出:
复制代码 代码如下:
400M
419430400
特别说明:$val = 400M时,case 'm' 被命中,其下的 $val *= 1024; 被执行,但因为没有 break 语言,所以会继续命中 case 'k',并执行其下的 $val *= 1024;语句,so,总体上相当于执行了 400 * 1024 * 1024 。
存入session,另一个页面取出session就可以了。
switch ($a) {
case "1" :
echo "A" ;
if(!isset($_SESSION)){
session_start();
}
$_SESSION['a']='A';
break;
另外一个页面:
if(!isset($_SESSION)){
session_start();
}
$a=$_SESSION['a'];
var iMonth=5;
var quarter=""
switch(iMonth){
case 1:;
case 2:;
case 3:;
quarter="chunji";
break;
case 4:;
case 5:;
case 6:;
quarter="xiaji";
break;
case 7:;
case 8:;
case 9:;
quarter="qiuji";
break;
case 10:;
case 11:;
case 12:;
quarter="dongji";
break;
}
document.write(quarter)

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

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.

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.

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.
