Home Backend Development PHP Tutorial php foreach/for loop jump problem_PHP tutorial

php foreach/for loop jump problem_PHP tutorial

Jul 13, 2016 pm 05:14 PM
for foreach php and exist cycle yes use Jump

In PHP, for loop and foreach are two commonly used functions. for is used for large numbers, while foreach is generally used for array traversal.

The code is as follows
 代码如下 复制代码

 

//php当前循环为1,循环由里到外依次递增,break默认为1,例如跳出第2层循环
for ($i=0;$i<3;$i++){
foreach (array(1,2,3) as $val){
foreach (array(1,2,3) as $val){
echo "1层循环<br/>"; 
            break 2;  //跳出第2层循环        
        }
        echo "2层循环<br/>";
    }
    echo "3层循环<br/>";
}
//结果:
//1层循环
//3层循环
//1层循环
//3层循环
//1层循环
//3层循环

Copy code

//The current loop of php is 1, and the loop increases from the inside to the outside. The default break is 1, for example, jumping out of the second layer of the loop
for ($i=0;$i<3;$i++){
foreach (array(1,2,3) as $val){
foreach (array(1,2,3) as $val){                                                                          echo "1 layer loop<br/>";
                                                                                                                                                                                break 2;                                }
             echo "2-layer loop<br/>";
}
echo "3-layer loop<br/>";
}
//Result:
//1 layer loop
//3-layer loop
//1 layer loop
//3-layer loop
//1 layer loop
//3-layer loop

http://www.bkjia.com/PHPjc/628950.htmlwww.bkjia.com
true
http: //www.bkjia.com/PHPjc/628950.html
TechArticleIn PHP, for loop and foreach are two commonly used functions. for is used for large numbers, and foreach Generally used in array traversal. The code is as follows Copy code //php current loop is 1, the loop is...
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 Article Tags

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 Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

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 Installation and Upgrade guide for Ubuntu and Debian

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

CakePHP File upload

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

CakePHP Routing

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

Discuss CakePHP

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

CakePHP Project Configuration

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

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

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles