endforeach in loops?

前端 未结 7 1476
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-01 05:59

I use brackets when using foreach loops. What is endforeach for?

相关标签:
7条回答
  • 2020-12-01 06:25

    How about that?

    <?php
        while($items = array_pop($lists)){
            echo "<ul>";
            foreach($items as $item){
                echo "<li>$item</li>";
            }
            echo "</ul>";
        }
    ?>
    
    0 讨论(0)
  • 2020-12-01 06:30

    It's mainly so you can make start and end statements clearer when creating HTML in loops:

    <table>
    <? while ($record = mysql_fetch_assoc($rs)): ?>
        <? if (!$record['deleted']): ?>
            <tr>
            <? foreach ($display_fields as $field): ?>
                <td><?= $record[$field] ?></td>
            <? endforeach; ?>
            <td>
            <select name="action" onChange="submit">
            <? foreach ($actions as $action): ?>
                <option value="<?= $action ?>"><?= $action ?>
            <? endforeach; ?>
            </td>
            </tr>
        <? else: ?>
             <tr><td colspan="<?= array_count($display_fields) ?>"><i>record <?= $record['id'] ?> has been deleted</i></td></tr>
        <? endif; ?>
    <? endwhile; ?>
    </table>
    

    versus

    <table>
    <? while ($record = mysql_fetch_assoc($rs)) { ?>
        <? if (!$record['deleted']) { ?>
            <tr>
            <? foreach ($display_fields as $field) { ?>
                <td><?= $record[$field] ?></td>
            <? } ?>
            <td>
            <select name="action" onChange="submit">
            <? foreach ($actions as $action) { ?>
                <option value="<?= $action ?>"><?= action ?>
            <? } ?>
            </td>
            </tr>
        <? } else { ?>
             <tr><td colspan="<?= array_count($display_fields) ?>"><i>record <?= $record['id'] ?> has been deleted</i></td></tr>
        <? } ?>
    <? } ?>
    </table>
    

    Hopefully my example is sufficient to demonstrate that once you have several layers of nested loops, and the indenting is thrown off by all the PHP open/close tags and the contained HTML (and maybe you have to indent the HTML a certain way to get your page the way you want), the alternate syntax (endforeach) form can make things easier for your brain to parse. With the normal style, the closing } can be left on their own and make it hard to tell what they're actually closing.

    0 讨论(0)
  • 2020-12-01 06:43

    How about this?

    <ul>
    <?php while ($items = array_pop($lists)) { ?>
        <ul>
        <?php foreach ($items as $item) { ?>
            <li><?= $item ?></li>
        <?php
        }//foreach
    }//while ?>
    

    We can still use the more widely-used braces and, at the same time, increase readability.

    0 讨论(0)
  • 2020-12-01 06:46

    as an alternative syntax you can write foreach loops like so

    foreach($arr as $item):
        //do stuff
    endforeach;
    

    This type of syntax is typically used when php is being used as a templating language as such

    <?php foreach($arr as $item):?>
        <!--do stuff -->
    <?php endforeach; ?>
    
    0 讨论(0)
  • 2020-12-01 06:47

    It's just a different syntax. Instead of

    foreach ($a as $v) {
        # ...
    }
    

    You could write this:

    foreach ($a as $v):
        # ...
    endforeach;
    

    They will function exactly the same; it's just a matter of style. (Personally I have never seen anyone use the second form.)

    0 讨论(0)
  • 2020-12-01 06:50

    Using foreach: ... endforeach; does not only make things readable, it also makes least load for memory as introduced in PHP docs So for big apps, receiving many users this would be the best solution

    0 讨论(0)
提交回复
热议问题