2011-07-06 29 views
10

我正在修改wordpress模板,我很好奇这是如何在PHP中有效的控制结构。任何人有任何见解?这个有效的php代码是怎么样的?

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

如果我删除所有的标签,我得到(更明智的缩进):

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

因为缩进if语句不结束这是无效的。那么为什么这个代码有效,如果有开始和结束php标签到处?


编辑为Kerrek SB。制作一个php文件并运行它。它是有效的:

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

我争辩说这是有效的。你成功运行它了吗?错误和所有? –

+0

@Kerrek SB - 是的,我测试了它。这是有效的,我不知道为什么。 –

+0

@cwolves:我也做了,并且我得到了'Parse error:语法错误,意外的T_ELSEIF' ...让我测试你的新例子。另外,你可以让它真正的代码,而不是伪代码? –

回答

4

你(减少)示例代码是相同的:

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

甚至:

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

通过关闭<?php标签,你会得到一个“空语句“ 免费。

真实的例子可以一个内衬像这样:

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

但是注意,elseif使得这个ambigous!

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

我们不得不这样歧义:

<?php 
if(condition): 
{ 
    if(!condition || !function()); 
} 
elseif(condition): 
{ 
    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中删除一个换行符?>后是否存在。 ?>[newline]<?php将是相同?><?php

有点像下面将更有意义(function因为function取代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; 
?> 

注意,有两个IFS没有:,那些会像if其预期旁边有一个身体进行治疗。写在另一种方式:

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

,但它确实工作:) –

+0

“功能”dooh,我碰到它,同时改善答案:) – Lekensteyn

+0

啊,是的抱歉。我只是意味着一个通用的函数:) –

-1

退房PHP.net的explaination

+1

我得到了什么'如果:'做的,我的问题是为什么嵌套的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

“这是无效的,因为缩进if语句不结束”

是他们做的。如php.net所述,不支持混合这两种表示法,这意味着您的if():,elseif():endif;中有一个新的代码块(两个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'构造的两种可能形式。

第一种形式可以是:

if (condition) instruction; 

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

第二个是:

if (condition): instruction; another; endif; 

当你写

<?php if (condition) ?> 

...没有 '{' 或' :'关闭'?>'标记之前,实际上你会截断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 

...在这里你有第一个如果可行,但第二个仍然是空的。

<?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){ }; ?> 

这又是一个例外,因为你不把关闭标签,并开始标记,所以解析器之间的任何东西“优化”出来(是不是完全一样,但它是一个正确的近似) 。尝试把两个PHP标签之间的任何东西,如:

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

...并看到“语法错误,意外的T_ELSEIF”出现。

相关问题