PHP break jumps out of multiple loop instances _php Tutorial

Source: Internet
Author: User
Break is used in the various loops and switch statements mentioned above. His role is to jump out of the current syntax structure and execute the following statement. The break statement can take a parameter n, which indicates the number of layers that jump out of the loop, and if you want to jump out of multiple loops, you can use N to indicate the number of layers that jumped out, or if the default is to jump out of the heavy loop without parameters.

Cases

The code is as follows Copy Code

$array = Array (1,2,3,4,5,6);
for ($i =0; $i <10; $i + +) {
foreach ($array as $key)
{
Echo $key;
if ($key ==2) {

Break 2;
}
}
}

Cases

The code is as follows Copy Code

Set the encoding to UTF-8 to avoid Chinese garbled characters
Header (' Content-type:text/html;charset=utf-8 ');

Executes a nested loop, with the outer loop 3 times, and the inner layer looping 2 times. When executing to the 2nd outer loop, use break to jump out of the inner layer loop.
for ($i = 0; $i < 3; $i + +) {
echo ' outer loop '. $i. ' Start
';
for ($j = 0; $j < 2; $j + +) {
if ($i = = 1) {
Break
}
Echo ' inner loop '. $i. '-' $j. '
';
}
echo ' outer loop '. $i. ' End

';
}
?>

The output page of the above code results in the following:

Outer Loop 0 Start
Inner Loop 0-0
Inner Loop 0-1
Outer Loop 0 End

Outer Loop 1 Start
Outer Loop 1 End

Outer Loop 2 Start
Inner Loop 2-0
Inner Loop 2-1
Outer Loop 2 End

The above code is easy to understand and break is used to jump out of the current level of the loop. However, unlike programming languages such as Java, in PHP, we can also follow the keyword break after a number, which represents the number of loop layers that need to jump out, so you can use a break statement, directly out of the multi-loop. For example:

The code is as follows Copy Code

Set the encoding to UTF-8 to avoid Chinese garbled characters
Header (' Content-type:text/html;charset=utf-8 ');

Executes a nested loop, with the outer loop 3 times, and the inner layer looping 2 times. When performing to the 2nd outer loop, use break 2 to jump out and jump directly out of the 2-layer loop
for ($i = 0; $i < 3; $i + +) {
echo ' outer loop '. $i. ' Start
';
for ($j = 0; $j < 2; $j + +) {
if ($i = = 1) {
Break 2; Jump directly out of a 2-layer loop using break 2
}
Echo ' inner loop '. $i. '-' $j. '
';
}
echo ' outer loop '. $i. ' End

';
}
?>

The above example code outputs the following page results:

Outer Loop 0 Start
Inner Loop 0-0
Inner Loop 0-1
Outer Loop 0 End

Outer Loop 1 Start

Therefore, in PHP, we can use a break followed by the specified number to jump directly out of the specified number of layers of the loop.
Note: A number that is followed by a break can only be a positive integer and cannot exceed the number of loop layers that can actually be jumped out. Otherwise, a fatal error (Fatal error) is reported.

http://www.bkjia.com/PHPjc/632635.html www.bkjia.com true http://www.bkjia.com/PHPjc/632635.html techarticle Break is used in the various loops and switch statements mentioned above. His role is to jump out of the current syntax structure and execute the following statement. The break statement can take a parameter n, which means that the jump ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.