You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP语法错误:unexpected 'endif' (T_ENDIF) 问题修复求助

修复PHP中unexpected 'endif' (T_ENDIF)语法错误

你遇到的这个语法错误其实很好定位——问题出在第二个if语句的结尾符号上。看这段代码:

<?php if( $video_banner ); ?>

这里你用了分号;来结束if的条件判断,但在PHP的替代语法(也就是if...endif这种写法)里,条件后面必须用冒号:,而不是分号。分号会让PHP认为这个if语句已经完全结束了,后面的endif就找不到对应的if结构,直接触发语法错误。

把分号改成冒号就能解决问题,修复后的完整代码如下:

<?php if( have_rows('home-video-slider') ): ?>
    <?php while( have_rows('home-video-slider') ): the_row(); 
        $video_banner = get_sub_field('video-banner');
    ?>
        <div class="item">
            <div class="fm-video-banner">
                <?php if( $video_banner ): ?>
                    <img src="<?php echo $video_banner['url']; ?>"/>
                <?php endif; ?>
                <a class="popup-youtube fm-play-icon" href="https://www.youtube.com/embed/5ktgB9hlETw">
                    <span>
                        <i class="fa fa-play" aria-hidden="true"></i>
                    </span>
                </a>
            </div>
        </div>
    <?php $p++; endwhile; ?>
<?php endif; ?>

另外给你个小提醒:使用PHP的替代语法(if: ... endif;、while: ... endwhile;这类)时,一定要记住条件后面用冒号,这样才能和对应的结束标签正确配对,避免这类低级语法错误。

内容的提问来源于stack exchange,提问作者sonu sasankan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:15:11