PHP if... else 替代语法抛出“unexpected ':' in..."错误信息 [英] PHP if... else alternative syntax throwing "unexpected ':' in..." error message
问题描述
我需要一些有关以下替代 if-else 语法的帮助.我经常使用它,它的成功率为 99.999999%.在这里,出于某种原因,它没有:
if ( get_row_layout() == 'spacer' ) :$spaceheight = get_sub_field('spacer_height');//替代语法:if...else 没有冒号的语句//一旦这两行被删除或更改为if ( some//条件 ) : 一些东西;endif;" 语法 >> 错误去//离开.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;行高:0;'></div>";elseif ( get_row_layout() == 'terms' ) ://一些其他的东西万一;
具体的错误信息是:解析错误:语法错误,意外的':' in...";并且在替代语法之后冒号是意外的,在 elseif ( get_row_layout() == 'terms' ) :
行中.
知道这里发生了什么吗?
<小时>@antoni 建议的答案是替代语法不能混合使用.但在这种情况下,为什么会这样?:
if ( $terms_primcont || $terms_seccont ) : ?><div class="terms__body-wrap"><?phpif ( $terms_primcont ) echo "<div class='terms__primary'>{$terms_primcont}</div>";if ( $terms_seccont ) echo "<div class='terms__secondary'>{$terms_seccont}</div>";?>
<?php endif;
发生这种情况是因为您混合了嵌套的 2 个语法 if.如果您这样做,它将起作用:
<小时>
问题编辑后:
它不能与 elseif
很好地混合在一起,你可以试试这个也有喙的:
$terms_primcont = 1;$terms_seccont = 1;如果( $terms_primcont || $terms_seccont ):echo "<div class=\"terms__body-wrap\">";如果( $terms_primcont )echo "<div class='terms__primary'>{$terms_primcont}</div>";如果( $terms_seccont )echo "<div class='terms__secondary'>{$terms_seccont}</div>";回声'</div>';否则 (1 === 2)//永远不要做某事.万一;
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;
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;
这篇关于PHP if... else 替代语法抛出“unexpected ':' in..."错误信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!