PHP的continue语句,PHP continue语句

本文概述

PHP continue语句用于继续循环。它继续程序的当前流程, 并在指定条件下跳过其余代码。

当你立即跳转到下一个迭代时, 将在循环和切换控制结构中使用continue语句。

continue语句可用于所有类型的循环, 例如-for, while, do-while和foreach循环。 Continue语句允许用户针对指定条件跳过代码的执行。

句法

下面给出了continue语句的语法:

jump-statement;

continue;

流程图:

0050303cc79a270827d377ddcddc1619.png

PHP Continue for循环示例

例子

在下面的示例中, 我们将仅打印i和j相同的那些值, 并跳过其他值。

//outer loop

for ($i =1; $i<=3; $i++) {

//inner loop

for ($j=1; $j<=3; $j++) {

if (!($i == $j) ) {

continue;//skip when i and j does not have same values

}

echo $i.$j;

echo "";

}

}

?>

输出

11

22

33

PHP继续while循环中的示例

例子

在下面的示例中, 我们将打印1到20之间的偶数。

//php program to demonstrate the use of continue statement

echo "Even numbers between 1 to 20: ";

$i = 1;

while ($i<=20) {

if ($i %2 == 1) {

$i++;

continue;//here it will skip rest of statements

}

echo $i;

echo "";

$i++;

}

?>

输出

Even numbers between 1 to 20:

2

4

6

8

10

12

14

16

18

20

PHP继续使用字符串数组的示例

例子

以下示例显示数组元素的值, 但指定条件为true且使用continue语句的元素除外。

$number = array ("One", "Two", "Three", "Stop", "Four");

foreach ($number as $element) {

if ($element == "Stop") {

continue;

}

echo "$element ";

}

?>

输出

One

Two

Three

Four

PHP继续带有可选参数的示例

Continue语句接受一个可选的数值, 该数值相应地使用。数值描述将退出多少个嵌套结构。

例子

请看以下示例, 以更好地理解它:

//outer loop

for ($i =1; $i<=3; $i++) {

//inner loop

for ($j=1; $j<=3; $j++) {

if (($i == $j) ) {//skip when i and j have same values

continue 1;//exit only from inner for loop

}

echo $i.$j;

echo "";

}

}

?>

输出

12

13

21

23

31

32

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值