我看到的是,你是混合了在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”出现。
我争辩说这是有效的。你成功运行它了吗?错误和所有? –
@Kerrek SB - 是的,我测试了它。这是有效的,我不知道为什么。 –
@cwolves:我也做了,并且我得到了'Parse error:语法错误,意外的T_ELSEIF' ...让我测试你的新例子。另外,你可以让它真正的代码,而不是伪代码? –