开发者

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

开发者 https://www.devze.com 2023-03-23 04:34 出处:网络
I want to display either a, b, or c. What\'s happening now is c always shows up, even if one of开发者_运维百科 the first two if statements is met.

I want to display either a, b, or c. What's happening now is c always shows up, even if one of开发者_运维百科 the first two if statements is met.

Thanks!

<?php

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

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

else  {
  echo 'c';
}

?>


In this case you can get

  • a,b
  • a,c
  • b
  • c

try

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


You're missing an else between the 'a' and 'b' sections:

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

is the basic structure you want. This'd only execute ONE of the a/b/c sections. As it stands right now, your code executes the 'a' section, then will execute the b OR c sections.


Consider changing the 2nd if to an else if and this should solve your problem, if an if statement fails it goes to the first else it can find in the chain (if there is one)


You should use elseif : http://www.php.net/manual/en/control-structures.elseif.php

0

精彩评论

暂无评论...
验证码 换一张
取 消