hansmans
hansmans

Reputation: 11

what is causing this endforeach error when I try to upgrade the site to PHP 7.4?

Hey This code of mine is causing a total crash on my website:

<select size="1" class="<?php echo $this->level; ?> mobilenav" onchange="document.location.href = '/'+this.value">

<?
if(/*$this->level == "level_1"*/ false) {
?>
    <option value="">Navigation...</option>
<?php foreach ($this->items as $item): 
  
    $classes = explode(" ",$item["class"]); 

    ?>

      <optgroup label="<?= $item["link"] ?>">
          <?= $item["subitems"] ?>
      </optgroup>
  <?php endforeach; ?>
<?
} else {
    foreach ($this->items as $item):

        $classes = explode(" ",$item["class"]);

        ?>
        <?php if ($item['isActive'] || in_array("trail",$classes)): ?>
        <option class="<?php echo $item['class']; ?>" selected="selected"><span class="<?php echo $item['class']; ?>"><?php echo $item['link']; ?></span></option>
    <?php else: ?>
        <option<?php if ($item['class']): ?> class="<?php echo $item['class']; ?>"<?php endif; ?> value="<?php echo $item['href']; ?>"><?php echo $item['link']; ?></option>
    <?php endif; ?>
    <?php endforeach;
}?>
</select>

Story: im Upgrading an old Website of mine currently to be able to use it with the newest version of PHP.

This is the error im getting:

[2021-07-27 08:08:38] request.CRITICAL: Uncaught PHP Exception ParseError: "syntax error, unexpected 'endforeach' (T_ENDFOREACH), expecting end of file" at /var/www/vhosts/web/templates/nav_dropdown.html5 line 20 {"exception":"[object] (ParseError(code: 0): syntax error, unexpected 'endforeach' (T_ENDFOREACH), expecting end of file at /var/www/vhosts/web/templates/nav_dropdown.html5:20)"} []

I dont get why it is expecting the code to end at line 20, maybe someone has a fast answer for me? thanks in advance. Hans.

Upvotes: 1

Views: 173

Answers (1)

IMSoP
IMSoP

Reputation: 97638

It looks like the error is not from the version of PHP, but the configuration: you have the short_open_tag setting turned off on your new server/installation.

Since you have a mixture of short (<?) and full (<?php) tags here, if short tags is off, PHP will only be reading half of them. To PHP, the if, else, and second foreach simply don't exist (they'll be part of the output), so it's as though you'd written this:

<select size="1" class="<?php echo $this->level; ?> mobilenav" onchange="document.location.href = '/'+this.value">

    <option value="">Navigation...</option>
<?php foreach ($this->items as $item): 
  
    $classes = explode(" ",$item["class"]); 

    ?>

      <optgroup label="<?= $item["link"] ?>">
          <?= $item["subitems"] ?>
      </optgroup>
  <?php endforeach; ?>

        <?php if ($item['isActive'] || in_array("trail",$classes)): ?>
        <option class="<?php echo $item['class']; ?>" selected="selected"><span class="<?php echo $item['class']; ?>"><?php echo $item['link']; ?></span></option>
    <?php else: ?>
        <option<?php if ($item['class']): ?> class="<?php echo $item['class']; ?>"<?php endif; ?> value="<?php echo $item['href']; ?>"><?php echo $item['link']; ?></option>
    <?php endif; ?>
    <?php endforeach;
}?>
</select>

Upvotes: 3

Related Questions