繁体   English   中英

我有两个if和else条件,但是else条件不起作用,总是显示

[英]I have two if and one else condition, but the else condition doesn't work and always shows

我想显示a,b或c。 现在发生的是,即使满足前两个if语句之一,c也会始终显示。

谢谢!

<?php

if(isset($_SESSION['member_email'])){
  echo 'a'; 
}

if ($user->isAuthorized())
{
  echo 'b'; 
}

else  {
  echo 'c';
}

?>

在这种情况下,您可以

  • A,B
  • A,C
  • b
  • C

尝试

if(isset($_SESSION['member_email'])){
  echo 'a'; 
} elseif ($user->isAuthorized()) {
  echo 'b'; 
} else  {
  echo 'c';
}

您在'a'和'b'部分之间缺少了else:

if (...) {
   a
} else if (...) {
   b
} else if (...) {
   c
}

是您想要的基本结构。 这只会执行a / b / c部分之一。 就目前而言,您的代码将执行'a'部分,然后将执行b OR c部分。

考虑将第二个if更改为else if,这应该可以解决您的问题,如果if语句失败,则转到链中可以找到的第一个else(如果有)

您应该使用elseif: http ://www.php.net/manual/en/control-structures.elseif.php

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM