Implementation of foreach () in PHP if you want to jump out of a loop or terminate a loop

Source: Internet
Author: User

This article brings you the content is about the PHP in the use of foreach () If you want to jump out of the loop or terminate the loop implementation method, there is a certain reference value, the need for friends can refer to, I hope to help you.

Example one:

In PHP, in a foreach () loop, you want to be in a loop, when a condition is met,

$arr = Array (' A ', ' B ', ' C ', ' d ', ' e '); $html = '; foreach ($arr as $key = = $value) {    if ($value = = ' B ') {        $html. = $ value;        Continue When the $value is B, jump out of the loop    }    if ($value = = ' C ') {        $html. = $value;        Break When $value is C, terminates the Loop    }    $html. = $value;} Echo $html; Output: AB

To jump out of this loop to continue with the next loop, or to satisfy a condition, terminate the foreach () loop, which will use: Continue and break.

Example two:

<?php$array = Array (1,2,3,4,5,6,7,8,9), foreach ($array as $value) {   echo $value;   if ($value = = 5)    {break        ;    }}?> results: 12345

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.