• Resolved hexley

    (@hexley)


    Can someone break down this php for me:
    if (have_posts()) : while (have_posts()) : the_post();

    I do not at all understand it, I pulled it right out of a current download from the default template.

    I see this one in the middle:
    <?php endwhile; else: ?>

    And then this:
    <?php endif; ?>

    I assume the last one is just a throwback to old php, and can be replaced with }

    But the rest, honestly, I have never seen that syntax.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Lots of good info: The_Loop (and the related links from there)

    Thread Starter hexley

    (@hexley)

    I looked in the loop, it is just that code style is as far as I can tell:
    https://nl3.php.net/manual/en/control-structures.alternative-syntax.php
    It is just strange to me, getting used to it.

    Do you mean that the following is difficult:

    if (blah) :
    while(blah) :
    ...
    endwhile;
    endif;

    I didn’t even know that PHP supported such a construct until I saw it in WP.. I was a C/C++ developer, so the opening/closing curly brackets were familiar. I eventually got used to the alternate PHP syntax, and have even used it at times.

    The other thing that sort of throws you is how the three statements are formatted in WP:

    For example:

    if (have_posts()) : while (have_posts()) : the_post();

    Note that this is really:

    if (have_posts()) :
        while (have_posts()) :
            the_post();
            ...
        endwhile;
    endif;

    For me, when I saw the alternate syntax, the fact that the three opening statements were on the same line made the issue more confusing.

    Thread Starter hexley

    (@hexley)

    Thanks ‘blogcms’, you nailed it, it is just ‘different’, I am getting used to it as I go along.

    You’re welcome! Definitely ‘different’.. ??

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘the loop, explained, by someone, do not get syntax’ is closed to new replies.