【问题标题】:How to breaking out of a loop if a condition is valid in PHP? [duplicate]如果条件在 PHP 中有效,如何跳出循环? [复制]
【发布时间】:2016-05-08 01:44:44
【问题描述】:

我搜索了类似的问题,但没有一个给出我正在寻找的答案。

这是脚本:

        <?php foreach ($images as $key => $image) {
            if (isset($_GET['locations']) && $_GET['locations'] == $key) {?>
                <img src="images/<?= $_GET['locations']; ?>" alt="" width=500px height=300>
                <figcaption><?= $image; ?></figcaption>
            <?php }
        } ?>

一旦点击的图片符合我的 if 语句的条件,我就试图跳出 foreach 循环,我该怎么做?

【问题讨论】:

    标签: php


    【解决方案1】:

    在您的 if 案例中,只需添加 break。如果您需要更多关于break的信息,可以阅读PHP Documentation

    <?php foreach ($images as $key => $image) {
        if (isset($_GET['locations']) && $_GET['locations'] == $key) {?>
            <img src="images/<?= $_GET['locations']; ?>" alt="" width=500px height=300>
            <figcaption><?= $image; ?></figcaption>
            <?php break; ?>
        <?php }
    } ?>
    

    【讨论】:

    猜你喜欢
    • 2013-02-21
    • 2014-01-23
    • 1970-01-01
    • 1970-01-01
    • 2020-02-20
    • 1970-01-01
    • 2016-04-08
    • 2010-09-23
    • 2011-02-14
    相关资源
    最近更新 更多