2011-07-06 21 views
10

私はwordpressテンプレートを変更しています。これがPHPでどのように有効なコントロール構造であるのか不思議です。誰もが洞察力を持っていますか?私はすべてのタグを削除した場合これはどのように有効なPHPコードですか?

<?php if(condition): ?> 
<?php if(!condition || !function()) ?> 
<?php elseif(condition): ?> 
<?php if(!condition || !function()) ?> 
<?php endif; ?> 

、私は(もっとまともなインデントで)取得する:インデントif文が終了していないため無効である

<?php 
if(condition): 
    if(!condition || !function()) 
elseif(condition): 
    if(!condition || !function()) 
endif; 
?> 

を。それで、どこにphpタグを開いたり閉じたりしている場合、どのように/なぜこのコードは有効でしょうか?


Kerrek SBの編集。 PHPファイルを作成して実行します。有効:

+3

私はこれが有効であると争う。あなたはそれを成功させましたか?すべてのエラー? –

+0

@Kerrek SB - はい、私はそれをテストしました。それは有効です、私は理由を知りませんでした。 –

+0

@cwolves:私もやったし、 'Parse error:syntax error、unexpected T_ELSEIF' ...新しい例を試してみましょう。また、擬似コードではなく、実際のコードにしてください。 –

答えて

4

あなたの(減少)のサンプルコードは、これと同等です:

<?php 
if(condition): 
    if(!condition || !function()) { } 
endif; 
?> 

あるいは:<?phpタグを閉鎖することにより

<?php 
if(condition): 
    if(!condition || !function()); 
endif; 
?> 

、あなたは「空のステートメントを取得するように見えます" 無料で。

あなたの本当の例がそうのような一並ぶことができます:

<?php if(true): if(true); endif; echo 'here'; ?> 

しかしelseifはこれが不明瞭になることに注意してください!

<?php 
if(condition): 
{ 
    if(!condition || !function()); 
} 
elseif(condition): 
{ 
    if(!condition || !function()); 
} 
endif; 
?> 

は今、それは明らかだが、今は完全に自分自身のコロン構文を免れることができた:

<?php 
if(condition): 
    if(!condition || !function()); 
elseif(condition): // Bogus! which block does this belong to? 
    if(!condition || !function()); 
endif; 
?> 

我々はこれを明確にする必要があるだろう。

これを指摘してくれたLekensteynに感謝します。

さらに奇妙なことについては以下の説明を参照してください。

+0

完全に真実ではなく、この場合をとってください: '<?php if(true):if (true)?><?php elseif(true):endif; ?>(構文エラー)vs '<?php if(true):if(true)?> XXX <?php elseif(true):endif; ?>( 'XXX'に注意してください)。これがバグか機能か分かりません。 – Lekensteyn

+0

これを計算していただきありがとうございます。あなたのエッジケースを悪用する話: –

+2

@Lekensteyn - ARGH! –

2

?>の後にPHPが改行を1つ削除するため、コードが機能しません。 ?>[newline]<?phpは、以下のような?><?php

何かがより多くの意味を作るのと同じになります(functionfunction以来fnに置き換えキーワードです;)):

<?php if(condition): ?> 
<?php if(!condition || !fn()) ?> 
some 
<?php elseif(condition): ?> 
<?php if(!condition || !fn()) ?> 
thing 
<?php endif; ?> 

これは、と解釈されたいです

<?php 
if(condition): 
    if(!condition || !fn()) echo "some"; 
elseif(condition): 
    if(!condition || !fn()) echo "thing"; 
endif; 
?> 

2つのifに:が存在しないことに注意してください。ifのように扱われます。他の方法で記述された:

<?php 
if (condition) { 
    if (!condition || !fn()) { 
     echo "some"; 
    } 
} else if (condition) { 
    if (!condition || !fn()) { 
     echo "thing"; 
    } 
} 
?> 
+0

それ以外は動作します:) –

+0

"function" dooh、私は答えを改善しながらそれを見つけました:) – Lekensteyn

+0

ああ、ええ、申し訳ありません。私は一般的な関数を意味しました:) –

-1

チェックアウトPHP.netのexplaination

+1

私は 'if:'を返します。私の質問はなぜネストされたif文がエラーを投げないのですか? –

0

あなたはあなたがフォローアップする必要が使用されていない場合は、次のロジックのために後の場合はネストされた(複数可)。

はい彼らはありません「インデントためのステートメントが終了しない場合は無効です」

<?php 
    function foobar() { 
      return true; 
    } 
    function bar() { 
      return false; 
    } 

    $foobar = true; 
    $bar = false; 

    if($foobar): 
     if(!$bar || !foobar()): 
      echo 'foobar'; 
     endif; 
    elseif($bar):  
     if(!$foobar || !bar()): 
      echo 'bar'; 
     endif; 
    endif; 
0

。 php.netで述べたように、両方の表記のミキシングはサポートされていません。つまり、if():elseif():endif;に新しいコードブロック(二重引用符がない2つのif())があります。

if(condition): 
    if(!condition || !function()) 
     // do nothing here, end of first inner if 
elseif(condition): 
    if(!condition || !function()) 
     // also do nothing here, end of second inner if 
endif; // end of outer if/else 
0

which is invalid because the indented if statements don't end

実際には、そうです。 ?>ので、他のどのような文を終了します。

<?php 
if(condition): 
    if(!condition || !function()); 
elseif(condition): 
    if(!condition || !function()); 
endif; 
?> 

はこれが当てはまらないし、次いで次も構文エラーになります:私が見る何

Lol <?php echo "cakes" ?> extravaganza <?php echo "innit" ?> 
0

は、あなたがアップミキシングされていることですphpで 'if'構造を構築する2つの可能な形式です。

if (condition) instruction; 

if (condition) { more; than; one; instructions; } 

秒1は、次のとおりです:

最初の形式は、どちらかであるあなたなし

<?php if (condition) ?> 
を...書く

if (condition): instruction; another; endif; 

'{' または ' : ''タグの前に '?'タグがありますが、実際にはifコマンドを省略して指示を省略しているので、PHPはこれを受け入れます。 PHPタグをもう一度開くと、すでにifの外側にいることになります。

単一のブロックで命令を「忘れる」場合には、同じことは適用されません。ここで、 'endif;命令として許可されていません。だから、

<?php if (condition) ?> 
anything outside the if 
<?php instruction_outside_the_if; ?> 

は...(あなたがいる場合手の込んだ、それの後に何もしないので)無意味ですが、厳密なエラーではありません。

<?php if (condition 1): ?> <!-- note the ':' in this if --> 
<?php if (condition 2) ?> 
anything outside the second if but inside the first 
<?php endif; ?> <!-- this closes the first if 

...ここでは動作しますが、2番目はまだ空です。

<?php if (condition 1): ?> <!-- note the ':' in this if --> 
<?php if (condition 2) ?> 
anything outside the second if but inside the first 
<?php else: ?> 
still outside the second if but inside the first, in the ELSE part 
<?php endif; ?> <!-- this closes the first if 

...これはelse(またはelseif)が最初のifに属しているあなたの例に多少似ています。


<?php if (true) ?> 
<?php elseif(foo){ }; ?> 

あなたは終了タグと開始タグの間では何も入れていないとして、別の例外ですので、パーサはそれらを「最適化」(正確にそのようではないですが、それは正しい近似です)それ。次のような2つのphpタグの間に何かを入れてみてください。

...「構文エラー、予期しないT_ELSEIF」が表示されます。

関連する問題