我正在通过特定国家的代码过滤我的网站上的一些内容,我正在尝试添加else语句,这样它就不必将每个部分作为单独的代码运行,除非我尝试给出一个错误:
<?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 }} ?>上面的代码给出了以下错误:Parse error: syntax error, unexpected T_ELSEIF in,并引用了第一个elseif
发布于 2010-06-27 00:28:27
或者在许多情况下使用switch-case会更好。例如:
switch($x) {
case 1:
?> some html <?php
break;
case 2:
?> more html <?php
break;
}您可以回显/打印html,而不是结束标记。
这将会起作用:
<?php if (function_exists('isCountryInFilter')) {
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 } elseif(isCountryInFilter(array("nz"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php }
}
?>发布于 2010-06-27 00:51:04
因为您正在将PHP与直接的HTML / OUTPUT相结合。在代码中,在结束括号和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块之外的空间不足。
这应该可以解决您的问题。
发布于 2010-06-27 00:27:57
Else不能计算条件,如果所有其他条件都为假,它将被执行,请将其视为开关的默认语句。
这一点:
<?php else(isCountryInFilter(array("nz"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php }} ?>需要是这样的:
<?php else if(isCountryInFilter(array("nz"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php }} ?>https://stackoverflow.com/questions/3124570
复制相似问题