Home Backend Development PHP Tutorial php如何跳出循环?

php如何跳出循环?

Jun 02, 2016 am 11:32 AM
php cycle

PHP跳出循环的方法:1、使用continue;2、使用break;3、使用goto;4、使用exit;5、使用return;6、在循环结束条件,自然跳出。

php如何跳出循环?

php如何跳出循环?下面本篇文章给大家介绍一下PHP跳出循环的几种方法。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。

PHP跳出循环的几种方法

PHP中的循环结构大致有for循环,while循环,do{} while 循环以及foreach循环几种,不管哪种循环中,在PHP中跳出循环大致有这么几种方式:

<?php
 
$i = 1;
while (true) { // 这里看上去这个循环会一直执行
    if ($i==2){// 2跳过不显示
       $i++;
       continue;
    } else if($i==5) {// 但到这里$i=5就跳出循循环了
       break;
    } else{
       echo $i . &#39;<br>&#39;;
    }
    $i++;
}
exit;
 
echo&#39;这里不输出&#39;;
?>
Copy after login

结果:

1
3
4
Copy after login

1、continue

continue是用来用在循环结构中,控制程序放弃本次循环continue语句之后的代码并转而进行下一次循环。continue本身并不跳出循环结构,只是放弃这一次循环。如果在非循环结构中(例如if语句中,switch语句中)使用continue,程序将会出错。

例如在下面的这段PHP代码片段中:

<?php
for($i = 1;$i <= 100; $i++ ){
if($i % 3 == 0 || $i % 7 == 0){
continue;
}
& #160;  else{
echo”$i \n<br/>”;
}
}
?>
Copy after login

PHP的代码片段的作用是输出100以内,既不能被7整除又不能被3整除的那些自然数,循环中先用if条件语句判断那些能被整除的数,然后执行continue;语句,就直接进入了下个循环。不会执行下面的输出语句了。

2、break

break是被用在上面所提的各种循环和switch语句中的。他的作用是跳出当前的语法结构,执行下面的语句。break语句可以带一个参数n,表示跳出循环的层数,如果要跳出多重循环的话,可以用n来表示跳出的层数,如果不带参数默认是跳出本重循环。

看下面这个多重循环嵌套的例子:

for($i = 1;$i <= 10; $i++ ){
    for($j = 1;$j <= 10;$j++){
        $m = $i * $i + $j * $j;
        echo”$m \n<br/>”;
        if($m < 90 || $m > 190) {
            break 2;
       }
    }
}
Copy after login

这里使用了break2跳出了两重循环,你可以试验一眼,将2去掉,得到的结果是完全不一样的。如果不使用参数,跳出的只是本次循环,第一层循环会继续执行下去。

3、goto

goto实际上只是一个运算符,和其他语言一样,PHP中也不鼓励滥用goto,滥用goto会导致程序的可读性严重下降。

goto的作用是将程序的执行从当前位置跳转到其他任意位置,goto本身并没有要结束的循环的作用,但其跳转位置的作用使得其可以作为跳出循环使用。

但PHP5.3及以上版本停止了对goto的支持,所以应该尽量避免使用goto。

下面的是一个使用了goto跳出循环的例子

for($i = 1000;$i >= 1 ; $i– ){
    if( sqrt($i) <= 29){
        goto a;
    }
    echo “$i”;
}
a:
echo” this is the end”;
Copy after login

例子中使用了goto来跳出循环,这个例子用来检测1000以内,那些数的平方根大于29。

4、exit

exit是用来结束程序执行的。可以用在任何地方,本身没有跳出循环的含义。exit可以带一个参数,如果参数是字符串,PHP将会直接把字符串输出,如果参数是integer整形(范围是0-254),那个参数将会被作为结束状态使用。

<?php
    for($i = 1000;$i >= 1 ; $i– ){
        if( sqrt($i) >= 29){
            echo”$i \n<br/>”;
        }
        else{
            exit;
        }
    }
    echo”本行将不会被输出”;
?>
Copy after login

上面这个例子中直接在从循环里结束了代码的运行,这样会导致后面的代码都不会被执行,如果是在一个php web页面里面,甚至连exit后面的html代码都不会被输出。

5、return

return语句是用来结束一段代码,并返回一个参数的。

可以从一个函数里调用,也可以从一个include()或者require()语句包含的文件里来调用,也可以是在主程序里调用,如果是在函数里调用程序将会马上结束运行并返回参数,如果是include()或者require()语句包含的文件中被调用,程序执行将会马上返回到调用该文件的程序,而返回值将作为include()或者require()的返回值。而如果是在主程序中调用,那么主程序将会马上停止执行

<?php
    for($i = 1000;$i >= 1 ; $i– ){
        if( sqrt($i) >= 29){
            echo”$i \n<br/>”;
        }
        else{
           return;
        }
    }
    echo”本行将不会被输出”;
?>
Copy after login

这里的例子和上面使用exit的效果是一样的。

6、在循环结束条件,自然跳出

这个当然是最好理解了,当循环满足循环临界条件时就是自己退出。

以上是PHP中跳出循环的几种方式的简单总结。

if不是循环结构,所以不能使用break,continue退出。如果你非要在if语句中这样用的话,可以把这个if语句放在一个循环结构中

$i=0;
$s=1;
while($i==0)
{
        if($s==1)
        {
                $v=8;
                break;
        }
        $v=9;
}
 
echo $v;
Copy after login

更多相关知识,请访问 PHP中文网!!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

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

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

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

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

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

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

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

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

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

See all articles