-1

I need some help with the below alternative if-else syntax. I use this very frequently and it works with a 99.999999% success rate. Here, for some reason, it doesn't:

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;

The specific error message is: "Parse error: syntax error, unexpected ':' in... "; and the colon is unexpected after the alternative syntax, in the elseif ( get_row_layout() == 'terms' ) : line.

Any idea what's happening here?


@antoni suggested the answer is that alternative syntaxes can't be mixed. But in this case why does this work?:

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;
miken32
  • 42,008
  • 16
  • 111
  • 154
Edward Munch
  • 149
  • 3
  • 9

1 Answers1

4

This happens because you are mixing nested 2 syntaxes if. If you do this it will work:

<?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] after question edit:

It doesn't mix well with elseif you can try this which also beaks:

$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; 
antoni
  • 5,001
  • 1
  • 35
  • 44
  • I'd like to think this is the right answer - but there's another similar code part where I use the same mixing ( see question update ), still it works fine. Only difference is an opening and closing HTML div tag. – Edward Munch Sep 10 '17 at 05:40
  • Thanks @antoni - accepted as the right answer. So summarized: it's specifically the elseif that doesn't play well with mixed syntaxes. Strange stuff, that's for sure!! – Edward Munch Sep 10 '17 at 11:00