endforeach in loops?

161,449

Solution 1

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.

Solution 2

It's the end statement for the alternative syntax:

foreach ($foo as $bar) :
    ...
endforeach;

Useful to make code more readable if you're breaking out of PHP:

<?php foreach ($foo as $bar) : ?>
    <div ...>
        ...
    </div>
<?php endforeach; ?>

Solution 3

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; ?>

Solution 4

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.)

Solution 5

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.

Share:
161,449
CyberJunkie
Author by

CyberJunkie

Updated on July 09, 2022

Comments

  • CyberJunkie
    CyberJunkie almost 2 years

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

  • Matthew Scharley
    Matthew Scharley over 13 years
    The second form is used a lot more when PHP is used in templates where there is 95% HTML and just a sprinkling of PHP control structures and echos.
  • Gromski
    Gromski over 13 years
    Personally I've never seen anyone use # as the comment operator in PHP... ;-)
  • CyberJunkie
    CyberJunkie over 13 years
    @Matthew, well, what is the advantage of the second form? I don't get it. To me it would seem simpler to just use {..}
  • BoltClock
    BoltClock over 13 years
    @CyberJunkie: I wouldn't be able to tell what <?php } ?> is closing.
  • Jason McCreary
    Jason McCreary over 13 years
    @deceze, # is another example of alternative syntax littered within PHP. Hate it!
  • cdhowie
    cdhowie over 13 years
    @deceze: I've seen it quite a bit -- since PHP syntax borrows a lot from Perl, it's common for Perl coders to use it. (# is the only comment token in Perl.) @Jason: Only this particular comment syntax is a lot more prominent. (It's actually documented for example.)
  • Gromski
    Gromski over 13 years
    @cdhowie That also explains why <?php } ?> doesn't make you run away screaming then. ;-)
  • cdhowie
    cdhowie over 13 years
    @deceze: It probably also explains my distaste for verbose languages like VB (or any BASIC, but especially VB) and Pascal.
  • phazei
    phazei about 12 years
    If you have bracket matching, then I find it easier to see { } even amid lots of HTML. Though perhaps it's easier for designers, but most tools support highlighted bracket matching.
  • Jason
    Jason about 11 years
    Agreed, @phazei, if you're not using an editor that matches the brackets, you're using the wrong editor.
  • Gavin
    Gavin over 10 years
    Good answer. I promote using the alternate form in templates. It is much cleaner, and conflicts less with the language it has been injected into. Relying on your IDE to make your messy code more readable is not really acceptable. Ultimately though I always see this debate as an extension of the bracing debate, and this alternate form is roughly equivalent to Allman style en.wikipedia.org/wiki/Indent_style#Allman_style with the balanced to the eye blocks of code that it presents. If you don't get or understand the benefits of Allman, you likely wont get the benefits of this either.
  • Erik van Velzen
    Erik van Velzen over 9 years
    Usually your IDE will not recognize that the strings contain HTML and it won't warn about non-matching tags.
  • elvismdev
    elvismdev almost 9 years
    I think you missed the dollar sign in the $action variable which outputs the value to the browser. Here the last action should be $action <option value="<?= $action ?>"><?= action ?>
  • Admin
    Admin about 6 years
    What is the advantage of writing it using alternative syntax?
  • Gromski
    Gromski about 6 years
    @Fahad Useful to make code more readable if you're breaking out of PHP