Home >Backend Development >PHP Tutorial >The role of break in php

The role of break in php

angryTom
angryTomOriginal
2019-08-23 13:54:563252browse

The role of break in php

break is a reserved word in some computer programming languages, and its function is to terminate the loop at the current level in most cases. In the switch (switch statement) of C language, the break statement can also be used to jump out of the current switch structure immediately after executing a case (branch). In the process of debugging some programs, break is used to set breakpoints. Next we will introduce the role of break in PHP.

Recommended tutorial: PHP video tutorial

##break ends the current for, foreach, while, do- Execution of while or switch structure.

break can accept an optional numeric parameter to determine how many loops to break out of.

<?php
    $arr = array(&#39;one&#39;, &#39;two&#39;, &#39;three&#39;, &#39;four&#39;, &#39;stop&#39;, &#39;five&#39;);
    while (list (, $val) = each($arr)) {
        if ($val == &#39;stop&#39;) {
            break; /* You could also write &#39;break 1;&#39; here. */
        }
        echo "$val<br />/n";
    }
/* Using the optional argument. */
    $i = 0;
    while (++$i) {
        switch ($i) {
            case 5:
                echo "At 5<br />/n";
                break 1; /* Exit only the switch. */
            case 10:
                echo "At 10; quitting<br />/n";
                break 2; /* Exit the switch and the while. */
            default:
                break;
            }
        }
?>

The above is the detailed content of The role of break in php. For more information, please follow other related articles on the PHP Chinese website!

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