The difference between break and continue in php

WBOY
Release: 2016-08-08 09:20:01
Original
1888 people have browsed it

The break statement is used in the loop body or switch, which means to jump out of the current loop. If break appears in switch, it jumps out of the current switch statement body of switch. In the for loop, you jump out of the current loop. And continue is this loop, skip the statements after this loop and continue with the next loop.
Eg: 99 multiplication table

<code><span>for</span>(<span>$i</span>=<span>1</span>;<span>$i</span><<span>10</span>;<span>$i</span>++){
        <span>for</span>(<span>$j</span>=<span>1</span>;<span>$j</span><=<span>$i</span>;<span>$j</span>++){
            <span>if</span>(<span>$j</span>==<span>3</span>)
                <span>//会跳过j=3的算式</span><span>continue</span>;
            <span>echo</span><span>$i</span>.<span>'x'</span>.<span>$j</span>.<span>'='</span>.<span>$i</span>*<span>$j</span>.<span>'    '</span>;
        }
        <span>echo</span><span>"<br/>";
    }</code>
Copy after login

Pulse result: When j=3, this loop will end and the next loop will continue. The column i*3 will not appear

<code><span>for</span>(<span>$i</span>=<span>1</span>;<span>$i</span><<span>10</span>;<span>$i</span>++){
        <span>for</span>(<span>$j</span>=<span>1</span>;<span>$j</span><=<span>$i</span>;<span>$j</span>++){
            <span>if</span>(<span>$j</span>==<span>3</span>)
                <span>break</span>;
            <span>echo</span><span>$i</span>.<span>'x'</span>.<span>$j</span>.<span>'='</span>.<span>$i</span>*<span>$j</span>.<span>'    '</span>;
        }
        <span>echo</span><span>"<br/>";
    }</code>
Copy after login

Result: When the loop reaches j=3 , will jump out of this loop, i*3 and the following columns will not be executed, so only the two columns i*1 and i*2 will appear in the result

Copyright statement: This article is an original article by the blogger, and has not yet been published No reproduction is allowed without the permission of the blogger.

The above introduces the difference between break and continue in PHP, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!