当前位置:网站首页>控制结构(二)
控制结构(二)
2022-04-23 15:27:00 【极梦网络无忧】
continue 语句
switch 语句
return 语句
include 语句
continue 语句
continue 在循环结构用来跳过本次循环中剩余的代码并在条件求值为真时开始执行下一次循环。
Note: 注意在 PHP 中 switch 语句被认为是可以使用 continue 的一种循环结构。
continue 接受一个可选的数字参数来决定跳过几重循环到循环结尾。默认值是 1,即跳到当前循环末尾。
编辑 /home/project/continue.php
<?php
$i = 0;
while ($i++ < 3) {
echo "Outer".PHP_EOL;
while (1) {
echo "Middle".PHP_EOL;
while (1) {
echo "Inner".PHP_EOL;
continue 3;
}
echo "This never gets output.".PHP_EOL;
}
echo "Neither does this.".PHP_EOL;
}
执行
php continue.php
switch 语句
当有多个 if elseif 条件时,可以使用 switch 来替换。
<?php
if ($a == 'apple') {
//do something
} elseif ($a == 'bnanan') {
//do something
} elseif ($a == 'oringe') {
//do something
} else {
//do something
}
使用 switch 替换为
<?php
switch ($a) {
case 'apple':
//do something
break;
case 'bnanan':
//do something
break;
case 'oringe':
//do something
break;
default:
//do something
}
switch 中每个条件需要一对 case 和 break。
default 匹配任何条件。如果匹配了某个条件,但是该语句中没有使用 break,则 default 中的语句将会被执行。
Note: 注意和其它语言不同,continue 语句作用到 switch 上的作用类似于 break。如果在循环中有一个 switch 并希望 continue 到外层循环中的下一轮循环,用 continue 2。
在 switch 语句中条件只求值一次并用来和每个 case 语句比较。在 elseif 语句中条件会再次求值。如果条件比一个简单的比较要复杂得多或者在一个很多次的循环中,那么用 switch 语句可能会快一些。
在一个 case 中的语句也可以为空,这样只不过将控制转移到了下一个 case 中的语句。
<?php
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 都不匹配的情况。例如:
<?php
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 表达式可以是任何求值为简单类型的表达式,即整型或浮点数以及字符串。不能用数组或对象,除非它们被解除引用成为简单类型。
switch 支持替代语法的流程控制。更多信息见控制结构(一)的替代语法一节。
<?php
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";
endswitch;
?>
return 语句
如果在一个函数中调用 return 语句,将立即结束此函数的执行并将它的参数作为函数的值返回。
<?php
function sayHello()
{
return "Hello";
echo "World";//不会被执行
}
echo sayHello();//Hello
return 可以不接任何参数。
<?php
function sayHello()
{
return;
}
include 语句
PHP 中 4 种包含语句
- include
- include_once
- require
- require_once
include 和 require 都可以加载文件,不同点在于如果加载的文件包含错误,include 发出警告,继续执行后面的语句,而 require 则发出致命错误,终止程序执行。
include_once 和 require_once 的区别同 include 和 require,都是遇到错误时是否继续执行。
include 和 include_once,以及 require 和 require_once 的区别在于是否进行重复检测。
<?php
include 'a.php';
require 'app/b.php';
包含文件执行顺序
- 参数是绝对路径(以 / 开头的路径),则直接包含该文件
<?php
include '/usr/local/share/a.php';
- 参数是相对路径或文件名,按照 include_path (可以通过 phpinfo() 查看当前包含的路径)指定的目录寻找。
- 如果在 include_path 下没找到该文件则在调用脚本文件所在的目录和当前工作目录下寻找。
- 如果最后仍未找到文件则 include 结构会发出一条警告;这一点和 require 不同,后者会发出一个致命错误。
变量范围
当一个文件被包含时,其中所包含的代码继承了 include 所在行的变量范围。从该处开始,调用文件在该行处可用的任何变量在被调用的文件中也都可用。不过所有在包含文件中定义的函数和类都具有全局作用域。
declare语句自行了解
版权声明
本文为[极梦网络无忧]所创,转载请带上原文链接,感谢
https://jimeng.blog.csdn.net/article/details/124298387
边栏推荐
- adobe illustrator 菜单中英文对照
- G007-HWY-CC-ESTOR-03 华为 Dorado V6 存储仿真器搭建
- On the day of entry, I cried (mushroom street was laid off and fought for seven months to win the offer)
- MySQL Basics
- 深度学习调参的技巧
- fatal error: torch/extension.h: No such file or directory
- Tencent has written a few words, Ali has written them all for a month
- API gateway / API gateway (III) - use of Kong - current limiting rate limiting (redis)
- Set onedrive or Google drive as a drawing bed in upic for free
- Lotus DB design and Implementation - 1 Basic Concepts
猜你喜欢

Mysql连接查询详解

Openstack theoretical knowledge

Byte interview programming question: the minimum number of K

Advanced version of array simulation queue - ring queue (real queuing)

Basic operation of circular queue (Experiment)

Mysql database explanation (VII)

Kubernetes详解(九)——资源配置清单创建Pod实战

木木一路走好呀
![Detailed explanation of C language knowledge points -- first understanding of C language [1] - vs2022 debugging skills and code practice [1]](/img/07/c534238c2b5405bbe4655e51cfee51.png)
Detailed explanation of C language knowledge points -- first understanding of C language [1] - vs2022 debugging skills and code practice [1]

2022年中国数字科技专题分析
随机推荐
setcontext getcontext makecontext swapcontext
Have you learned the basic operation of circular queue?
Openstack command operation
Tun equipment principle
Detailed explanation of kubernetes (XI) -- label and label selector
Differential privacy (background)
PSYNC synchronization of redis source code analysis
MultiTimer v2 重构版本 | 一款可无限扩展的软件定时器
Adobe Illustrator menu in Chinese and English
Explanation 2 of redis database (redis high availability, persistence and performance management)
How to design a good API interface?
Nuxt project: Global get process Env information
激活函数的优缺点和选择
Design of digital temperature monitoring and alarm system based on DS18B20 single chip microcomputer [LCD1602 display + Proteus simulation + C program + paper + key setting, etc.]
After time judgment of date
深度学习调参的技巧
JUC学习记录(2022.4.22)
MySQL installation process (steps for successful installation)
Redis master-slave synchronization
Sword finger offer (2) -- for Huawei