当我尝试将网站升级到PHP7.4时,是什么导致了这个endforeach错误?

jmp7cifd  于 2021-10-10  发布在  Java
关注(0)|答案(1)|浏览(258)

嘿,我的这段代码导致我的网站彻底崩溃:

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

故事:我正在升级我的一个老网站,以便能够使用最新版本的php。
这是我得到的错误:

[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)"} []

我不明白为什么它期望代码在第20行结束,也许有人能给我一个快速的答案?提前谢谢。汉斯。

3bygqnnd

3bygqnnd1#

看起来错误不是来自php版本,而是来自配置:您在新服务器/安装上关闭了short_open_标记设置。
因为你有一个混合的短( <? )满满的( <?php )这里的标签,如果短标签关闭,php将只读取其中的一半。对于php来说 if , else ,第二 foreach 根本不存在(它们将是输出的一部分),所以就好像您写了这样的内容:

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

相关问题