继续使用 PHP

WBOY
发布: 2024-08-28 17:46:57
原创
499 人浏览过

Continue 语句是在条件语句中使用的语句,指示代码跳过正在进行的循环的剩余迭代,并在条件评估时继续执行并跳转到下一次执行的开始。在 PHP 中,最常见的地方是 switch 语句中使用 continue 语句。

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

Continue 采用一个可选的数字参数,该参数定义执行应跳过多少层内部循环并转到末尾。这意味着如果该值为 1,则它会跳到当前循环的末尾。 Break 语句用于完全结束循环,而 continue 则用作从当前循环进入下一个循环的快捷方式。两者都为循环上的编码器提供了额外的控制,以按照他们想要的方式操作它。

语法:

while ( condition 1)
{
//declaration statements
if ( condition 2 )
{
//PHP code goes here
continue ;
}
// Operational Statements
}
登录后复制

如上所示,我们首先声明一个 while 循环及其条件。当条件满足时,代码进入循环,一直持续到条件不成立为止。仅当条件为 true 时,代码才会进入 if 语句循环。这里的 continue 语句会跳过本次迭代,不会执行 continue 之后的后面部分代码。因此,控制权转移到条件为 1 的 while 循环的下一次迭代。

流程图

以下是 PHP 中 continue 的流程图:

继续使用 PHP

如流程图所示,Continue 始终出现在执行循环的代码内的语句块中。如果 continue 的条件为真,它将跳过后续操作并跳到下一次迭代。

PHP 中的Continue 示例

让我们通过一些详细的示例来了解 continue 语句的确切工作原理,如下所示:

示例#1

代码:

<?php
$a = 0;
for ($a = 0;$a <= 10;$a++)
{
if ($a==4)
{
break;
}
echo $a;
echo "\n";
}
echo "End of for loop" ;
?>
登录后复制

输出:

继续使用 PHP

在这个程序中,我们首先将变量 a 的值初始化为零。然后,我们使用 for 循环将其值加一,直到其值达到 10。另一个条件语句 if 循环也用于在 a 的值达到等于 4 时中断此循环。因此,它会跳出循环并然后打印“循环结束”,而不是打印值 5。为了更好地理解,我们打印输出中每个递增的值。正如您所看到的,由于break语句,输出停止在4处。

现在让我们看看 continue 语句对相同代码做了什么:

代码:

<?php
$a = 0;
for ($a = 0;$a <= 10;$a++)
{
if ($a==4)
{
continue;
}
echo $a;
echo "\n";
}
echo "End of for loop" ;
?>
登录后复制

输出:

继续使用 PHP

正如您在上面的输出中看到的,程序像上一个一样打印了从 0 到 4 的数字。当遇到 if 条件语句并且它为 TRUE 时,由于给出了 continue 语句,它跳过了打印 4 但再次继续循环。因此,在输出中,我们看不到 4,但增量再次从 5 开始,直到 10,这是 for 循环条件,然后打印“End of for loop”,表明它已脱离 for a 循环。因此, continue 通常用于从该特定实例跳过并从下一个实例继续循环。

示例#2

代码:

<?php
$a=0;
foreach ($a as $k => $val) {
if (!($k % 2)) { // skip even members
continue;
}
do_something_odd($val);
}
$j = 0;
while ($j++ < 5) {
echo "Outermost while loop\n";
while (1) {
echo "Middle while loop\n";
while (1) {
echo "Innermost while loop\n";
continue 3;
}
echo "This text will never get printed\n";
}
echo "This text also will never get printed\n";
}
?>
登录后复制

输出:

继续使用 PHP

在上面的代码中,我们在 if 条件循环中使用变量 k 通过使用 modulo 2 函数来跳过偶数。我们在这里使用 continue 语句,它会跳过当前迭代,因此这里不会打印任何输出。

在循环的第二部分中,我们使用变量 j 并首先将其初始化为零。然后我们使用 3 个 while 循环:最外面的循环、中间的循环和最里面的循环。最外层循环的条件是当变量 j 小于 5 并且 j 不断递增 1 时执行。中间和内层循环是无限的,这意味着它们在没有任何条件的情况下继续执行。

Just like the break statement, we can also pass numeric parameters along with the continue statement. As shown in the above example, we are passing the number 3 which means that it skips 3 iterations of the loop. Hence the text which will never get printed is shown in the code. The same code when we give continue 2 will skip the innermost and the middle while loop and prints the last text. Similarly, continue 1 will skip only the innermost loop. However, note that continues 0 is not a valid statement to execute even though it was valid before as it was considered to be the same as continue 1.

Example #3

Continue can also be used in a switch statement as follows:

Code:

<?php
echo"\n";
echo"\n";
for ( $a = 0; $a < 5; $a++ ) {
switch ($a)
{
case 0:
echo $a . "\nb\n";
continue 2;
echo $a . "\na\n";
case 1:
echo $a . "\nb\n";
continue 2;
echo $a . "\na\n";
case 2:
echo $a . "\nb\n";
break;
echo $a . "\na\n";
}
echo "end\n";
}
echo"\n";
echo"\n";
?>
登录后复制

Output:

继续使用 PHP

It can be seen that in a switch statement, break and continue statements work in the same way. But in the loops, they are used to stop the loop iteration and move on to the next. By using continue 2 we can come out of the switch inside a loop and continue to the next outer loop iteration.

Importance of Continue in PHP

  1.  The main purpose of the continue statement is inside loops for skipping a particular instance of iteration where the coder wants it.
  2. In case of a do-while loop, the continue statement takes control of the conditional statement part of the while loop.
  3. Even in a while loop, the continue again takes control of the condition of while loop.
  4. In for loop, the increment or decrement actions get executed after the continue statement.

Disadvantages of Continue in PHP

  1. We cannot use continue in a file name which is inside a loop as it will cause an error.
  2. Using continue makes the code hard to follow and does not look elegant.

Conclusion – Continue in PHP

Continue statements are majorly used in all kinds of loops or conditional statements in PHP. They basically stop the ongoing iteration of that loop but does not terminate the same. Continue statement present inside the statement block just hops on to the next iteration which succeeds in the conditional statement.

以上是继续使用 PHP的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
php
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!