search
Homephp教程php手册php中跳出函数break,continue,exit的使用与区别

php中跳出函数break,continue,exit的使用与区别

Jun 12, 2016 pm 08:07 PM
breakcontinuephpcodeOpen sourceprogrammingprogramming languagesoftware development

1.break跳出代码片段,结束这个循环 

2.continue结束当前片段,结束这次循环,继续下一次循环

3.exit 结束整个PHP代码

 
<span style="font-family: tahoma, arial, helvetica, sans-serif; font-size: 14px;"><span style="line-height: 19px;">break的作用是跳出这个循环(如果这个break或者continue在循环中的if语句里面,不是跳出if语句,而是跳出循环语句),</span><span style="line-height: 19px;">执行这个循环的大括号以后的语句,</span></span>
<span style="font-family: tahoma, arial, helvetica, sans-serif; font-size: 14px;"><span style="line-height: 19px;">break在循环语句中是这样,在switch语句用功能也是这样的,而continue是当条件满足的时候不执行这个循环后面的语句,从这个循环的开始重新执行。</span></span>
 
<?php   
/* php的break,continue,return 的简单区别代码 */   
$i = 1;     
while (true) { // 这里看上去这个循环会一直执行     
    if ($i==2) {// 2跳过不显示     
        $i++;     
        continue;     
    } else if ($i==5) {// 但到这里$i=5就跳出循循环了     
        break;     
    } else {     
        echo $i . '<br>';     
    }     
    $i++;     
}     
exit;     
     
echo '这里不输出'; 
?>

 

Statement
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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor