【问题标题】:PHP if... else alternative syntax throwing "unexpected ':' in..." error messagePHP if... else 替代语法抛出“unexpected ':' in...”错误信息
【发布时间】:2017-09-10 01:48:26
【问题描述】:

我需要以下替代 if-else 语法的帮助。 我经常使用它,它的成功率高达 99.999999%。 在这里,由于某种原因,它没有:

if ( get_row_layout() == 'spacer' ) : 

    $spaceheight = get_sub_field('spacer_height');

    // The Alternative Syntax: if... else statement without a colon
    // Once these two lines are removed OR changed into "if ( some
    // condition ) : some stuff; endif;" syntax >> the error goes
    // away. 
    if ( $spaceheight && ( 0 !== $spaceheight ) )
        echo "<div class='basf-spacer' style='display: block; width: 100%; height: {$spaceheight}px; background: transparent; padding: 0; margin: 0; font-size: 0; line-height: 0;'></div>";

elseif ( get_row_layout() == 'terms' ) :

    // Some other stuff

endif;

具体报错信息为:“Parse error: syntax error, unexpected ':' in...”;并且冒号在替代语法之后是意外的,在elseif ( get_row_layout() == 'terms' ) : 行中。

知道这里发生了什么吗?


@antoni 建议的答案是不能混合使用替代语法。 但在这种情况下,为什么会这样呢?:

if ( $terms_primcont || $terms_seccont ) : ?>

    <div class="terms__body-wrap">

        <?php 
        if ( $terms_primcont ) echo "<div class='terms__primary'>{$terms_primcont}</div>";
        if ( $terms_seccont ) echo "<div class='terms__secondary'>{$terms_seccont}</div>";
        ?>

    </div>

<?php endif;

【问题讨论】:

    标签: php if-statement syntax parse-error php-parse-error


    【解决方案1】:

    发生这种情况是因为您混合了嵌套的 2 种语法 if。如果你这样做,它将起作用:

    <?php
    function get_row_layout(){}
    
    if ( get_row_layout() == 'spacer' ) :
    
        $spaceheight = get_sub_field('spacer_height');
    
        // The Alternative Syntax: if... else statement without a colon
        // Once these two lines are removed OR changed into "if ( some
        // condition ) : some stuff; endif;" syntax >> the error goes
        // away.
        if ( $spaceheight && ( 0 !== $spaceheight ) ) :
            echo "<div class='basf-spacer' style='display: block; width: 100%; height: {$spaceheight}px; background: transparent; padding: 0; margin: 0; font-size: 0; line-height: 0;'></div>";
        endif;
    elseif ( get_row_layout() == 'terms' ) :
    
        // Some other stuff
    
    endif;
    
    ?>
    

    [EDIT] 问题编辑后:

    它与elseif 混合不好,你可以试试这个也有喙:

    $terms_primcont = 1;
    $terms_seccont = 1;
    if ( $terms_primcont || $terms_seccont ) :
    
        echo "<div class=\"terms__body-wrap\">";
    
    
            if ( $terms_primcont )
                echo "<div class='terms__primary'>{$terms_primcont}</div>";
            if ( $terms_seccont )
                echo "<div class='terms__secondary'>{$terms_seccont}</div>";
    
    
        echo '</div>';
    
    elseif (1 === 2)
        // never do sth.
    endif; 
    

    【讨论】:

    • 我想这是正确的答案 - 但是还有另一个类似的代码部分,我使用相同的混合(请参阅问题更新),仍然可以正常工作。唯一的区别是打开和关闭 HTML div 标签。
    • 感谢@antoni - 被接受为正确答案。总结如下:特别是 elseif 在混合语法中表现不佳。奇怪的东西,这是肯定的!
    猜你喜欢
    • 2015-05-29
    • 2015-01-05
    • 1970-01-01
    • 1970-01-01
    • 2020-12-09
    • 2022-11-30
    • 1970-01-01
    • 2018-03-29
    • 1970-01-01
    相关资源
    最近更新 更多