2017-09-10 19 views
-1

if-else構文の代わりにいくつかの助けが必要です。 私はこれを頻繁に使用し、99.999999%の成功率で動作します。ここで 、何らかの理由で、それはしていません:PHP if ... else alternative syntax throw "unexpected": 'in ... "エラーメッセージ

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; 

特定のエラーメッセージがある:「解析エラー:構文エラー、予期しない 『:』で...」。代わりのシンタックスの後のコロンは、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; 

答えて

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; 

?> 

質問編集後[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; 
+0

これは正しい答えだと思っていますが、同じミキシング(質問の更新を参照)を使用する別の同様のコード部分がありますが、それでも問題ありません。違いは、HTMLのdivタグを開閉することだけです。 –

+0

ありがとう@antoni - 正しい答えとして受け入れられました。 要約すると、それは特に、混合構文ではうまくいかないelseifです。奇妙なもの、それは確かです! –