首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP意外的其他情况

PHP意外的其他情况
EN

Stack Overflow用户
提问于 2010-06-27 00:19:29
回答 5查看 4K关注 0票数 2

我正在通过特定国家的代码过滤我的网站上的一些内容,我正在尝试添加else语句,这样它就不必将每个部分作为单独的代码运行,除非我尝试给出一个错误:

代码语言:javascript
复制
<?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

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2010-06-27 00:28:27

或者在许多情况下使用switch-case会更好。例如:

代码语言:javascript
复制
switch($x) {
  case 1:
  ?> some html <?php
  break;
  case 2: 
  ?> more html <?php
  break;
}

您可以回显/打印html,而不是结束标记。

这将会起作用:

代码语言:javascript
复制
<?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 }
}
?>
票数 2
EN

Stack Overflow用户

发布于 2010-06-27 00:51:04

因为您正在将PHP与直接的HTML / OUTPUT相结合。在代码中,在结束括号和elseif关键字之间打印空格。

PHP的解释器直接在}后查找elseif关键字,但它找到的是一块输出数据,因此它会引发一个错误。

代码语言:javascript
复制
<?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 }}  ?>

您需要做的就是像这样删除空格。

代码语言:javascript
复制
<?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块之外的空间不足。

这应该可以解决您的问题。

票数 4
EN

Stack Overflow用户

发布于 2010-06-27 00:27:57

Else不能计算条件,如果所有其他条件都为假,它将被执行,请将其视为开关的默认语句。

这一点:

代码语言:javascript
复制
<?php else(isCountryInFilter(array("nz"))) { ?>
    <a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
    <?php }}  ?>

需要是这样的:

代码语言:javascript
复制
<?php else if(isCountryInFilter(array("nz"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php }}  ?>
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3124570

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档