PHP如果......其他语法抛出"意外':'在..."错误信息

时间:2017-09-10 01:48:26

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

我需要以下替代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;

具体的错误信息是:&#34;解析错误:语法错误,意外&#39;:&#39;在...&#34 ;;在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;

1 个答案:

答案 0 :(得分:4)

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

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

?>
问题编辑后

[编辑]:

它与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;