PHP意外elseif
我正在通过特定国家/地区的代码过滤网站上的一些内容,我正在尝试添加else语句,这样它就不必将每一部分作为单独的代码运行,除非我尝试的内容会出现错误:PHP意外elseif,php,if-statement,Php,If Statement,我正在通过特定国家/地区的代码过滤网站上的一些内容,我正在尝试添加else语句,这样它就不必将每一部分作为单独的代码运行,除非我尝试的内容会出现错误: <?php if (function_exists('isCountryInFilter')) { ?> <?php if(isCountryInFilter(array("us", "ca"))) { ?> <a rel="nofollow" class='preloading gallery_image' hre
<?php if (function_exists('isCountryInFilter')) { ?>
<?php if(isCountryInFilter(array("us", "ca"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php } ?>
<?php elseif(isCountryInFilter(array("au"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php } ?>
<?php else(isCountryInFilter(array("nz"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php }} ?>
上面给出了以下错误:
解析错误:语法错误,在
中出现意外的T_ELSEIF,并引用了第一个ELSEIF,而不是使用该格式
<?php if($foo == 1): ?>
html code
<?php else if($foo == 2): ?>
other html
<?php else: ?>
more html code
<?php end; ?>
html代码
其他html
更多html代码
否则无法计算条件,如果所有其他条件都为false,则将执行该条件,请根据开关的默认状态来考虑
这:
需要这样做:
<?php else if(isCountryInFilter(array("nz"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php }} ?>
甚至更好地在许多情况下使用开关盒。 例如:
switch($x) {
case 1:
?> some html <?php
break;
case 2:
?> more html <?php
break;
}
开关($x){
案例1:
?>一些html或更多html
FrEaKmAn的答案可能有用,但我自己更喜欢花括号。试着换一下
<?php } ?>
<?php elseif(isCountryInFilter(array("au"))) { ?>
到
另外,一般来说,最好尽量减少您进出php块的次数
(修复此部分后,您将需要处理DamienL指出的问题。)将PHP与直接HTML/输出相结合。在代码中,您将在结束括号和elseif关键字之间打印空格 PHP的解释器直接在}后面查找elseif关键字,但它发现的是一个输出数据块,因此会引发错误
<?php if (function_exists('isCountryInFilter')) { ?>
<?php if(isCountryInFilter(array("us", "ca"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php } ?>
<!--PHP Finds space here which it does not expect.-->
<?php elseif(isCountryInFilter(array("au"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php } ?>
<!--PHP Finds space here which it does not expect.-->
<?php elseif(isCountryInFilter(array("nz"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php }} ?>
您需要做的是像这样删除空白
<?php if (function_exists('isCountryInFilter')) { ?>
<?php if(isCountryInFilter(array("us", "ca"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php }elseif(isCountryInFilter(array("au"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php }else(isCountryInFilter(array("nz"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php}}?>
您将注意到PHP块外缺少空间
这应该可以解决您的问题。非常正确,但OPs描述听起来好像解析器甚至没有做到这一点…@sAc:这种风格是许多人喜欢的这种类型的编码(我仍然认为有意大利面的味道),因为它消除了对花括号的需要。因为花括号是OP被绊倒的原因,所以这是一个合理的建议。这是当你将php逻辑与html相结合时得到的结果,这与他在上面使用的方法没有什么不同。是的,但至少在这种情况下,花括号不会让人头疼,这很烦人。这对初学者有好处。然而,对于一个好的项目来说,最好实现一些模板系统,比如Smarty。您应该真正了解如何将逻辑与视图/输出分离。不管怎样,不仅要检查我的回答。else(isCountryInFilter(数组(“nz”))错误。如果所有语句都为false并且不能包含任何条件,则将运行Else。它会给你一个错误。那么结尾呢:这也会给你一个错误,因为两者之间没有空格。这是真的,但我把我的评论集中在手边的错误上,一旦他在我上面的帮助下纠正了错误,他将自己处理第二个问题。但是你是对的,他需要把else的执行过程用括号括起来。我刚刚复制了他的代码块并修复了空白问题,else的另一个问题会抛出一个php E_解析错误,所以他会自己跟踪:/感谢所有的答案我发现这个建议以及DamienL的答案修复了我的错误。
<?php } elseif(isCountryInFilter(array("au"))) { ?>
<?php if (function_exists('isCountryInFilter')) { ?>
<?php if(isCountryInFilter(array("us", "ca"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php } ?>
<!--PHP Finds space here which it does not expect.-->
<?php elseif(isCountryInFilter(array("au"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php } ?>
<!--PHP Finds space here which it does not expect.-->
<?php elseif(isCountryInFilter(array("nz"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php }} ?>
<?php if (function_exists('isCountryInFilter')) { ?>
<?php if(isCountryInFilter(array("us", "ca"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php }elseif(isCountryInFilter(array("au"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php }else(isCountryInFilter(array("nz"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php}}?>