1

I've inherited a custom template via a new client and have a piece of code that I can't get past. It's working fine on the live server but I can't get the site to load on my local setup because I keep getting this error:

Parse error: syntax error, unexpected 'endwhile' (T_ENDWHILE) in template-welcome.php on line 88

The original developer says it matches the code in his file and he's not having any issues with it so I'm quite perplexed.

I'm running XAMPP on Windows. If I comment out the first endwhile statement I get the same error but refering to a line lower down in the code. I think that the endwhile has been uncoupled from the if statement but I can't see how to fix it.

Any advice? The section of code, from the original if to the endwhile statement, is copied below.

<?php
// check if the repeater field has rows of data
if (have_rows('sections')):
    // loop through the rows of data
    $section_counter = 1;
    while (have_rows('sections')) : the_row();
        ?>
        <div class="row">
            <div class="col-md-12">
                <h3 class="heading"><?php the_sub_field('section_title'); ?></h3>
                <?php the_sub_field('section_content'); ?>
                <?
                $section_slider_images = get_sub_field('section_slider');
                ?>
                <div class="home-section-slider owl-carousel owl-theme">
                    <?php
                    foreach ($section_slider_images as $image) {
                        ?>
                        <div class="item"><img class="lazyOwl"
                             data-src="<?php echo $image['url']; ?>"
                                  alt="<?php echo $image['alt']; ?>"
                                  title="<?php echo $image['title']; ?>"></div>
                        <?
                    }
                    ?>
                </div>
            </div>
        </div>
        <?php
        $section_counter++;
    endwhile;
else :
    // no rows found
endif;
?>

closed as off-topic by fuxia Jun 12 '18 at 10:44

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • Looks OK at the first glance. Although it is a little bit messy and using short php tags is rather risky. Line 88 is the line with endwhile; in it? – Krzysiek Dróżdż Jun 12 '18 at 7:20
  • Yes, that's it. I'll edit my question so that's clear. I've tried commenting out endwhile, then the error flags else. If I comment out else, the error flags endif, which is why think something is missing between the if and the endwhile. I also tried commenting out the } halfway down but that makes no difference so I'm not sure why that's there. – Emma Jun 12 '18 at 8:07
  • i'd also go for the short-tags. don't use those. always write <?php instead of simply <? – honk31 Jun 12 '18 at 8:08
  • Ah-hah! I thought I'd caught all of those but just went through again step by step and found two instances. It works!! Thank you! – Emma Jun 12 '18 at 8:12
  • @fuxia I'm new to this site. Can you tell me why this question isn't relevant here. If I call it "Wordpress site won't display in the local server" can it stay? Or is it better left as-is and moved to the PHP Stack? Thanks in advance for your advice. – Emma Jun 12 '18 at 21:23
1

Check your short_open_tag setting in php.ini. You have a mixture in this code. If the setting is switched off, it may be that the statements using short tags are not being parsed.

This, in particular, maybe causing the loops to be "out of sync"

                        <?
                }
                ?>

If short_open_tags is off, the foreach loop will not be looped and encounter an endwhile prematurely.

  • Okay, thank you. Yes, I just checked and it's set to Off for both Development and Production. Is it better for me to enable it or to fix all the code so that there aren't any short tags? (As I said, I inherited this template so it's not my code!) – Emma Jun 12 '18 at 8:15
  • Either way, since alternating control syntax within a block is "not supported" in PHP. Also,it makes for more readable code if you maintain consistency at least within a major block. – CK MacLeod Jun 12 '18 at 8:18
  • I agree with the readability statement from @CKMacLeod. It can be turned on if you just simply need to make the application work. Then ensure that going forward you adopt using the full <?php open tag and change any that you find. – Zakalwe Jun 12 '18 at 8:22

Not the answer you're looking for? Browse other questions tagged or ask your own question.