2017-08-06 48 views
-1

有没有办法为if/else语句添加按钮?这是我的代码如何在if(isset)之后添加按钮?

<?php if(isset($_SESSION["steamname"])) 
     //If steamname not equals 0 
      { 
        <a class="button-logout" href="steamauth/logout.php">Log Out</a> 

       } 
      else 
       { 
        <a class="button-login" href="steamauth/login_steam.php">Log In</a> 

       } 
     ?> 

但我的服务器一直说这是无效的。我对php的理解并不是那么好,但我想要做的就是让用户登录时注销按钮将会出现,如果没有,将会登录。我目前的方法不起作用,甚至有可能?谢谢。

P.S.我试着回应它,也没有运气。我认为这与我的isset命令没有任何关系。我做了一个简单的回声,它工作得很好。

+0

你确实需要回显按钮。为什么这不适合你? – rickdenhaan

+0

我不知道,当我这样做时,它只是说“意外”<“”等等,直到我的整个命令行消失。这可能是我的格式。你有我可以尝试的代码吗? – Garlicvideos

+2

[PHP Parse/Syntax Errors;和如何解决它们?](https://stackoverflow.com/questions/18050071/php-parse-syntax-errors-and-how-to-solve-them) –

回答

3

你需要回应你想要的HTML:

<?php if(isset($_SESSION["steamname"])) 
    //If steamname not equals 0 
     { 
       echo '<a class="button-logout" href="steamauth/logout.php">Log Out</a>'; 

      } 
     else 
      { 
       echo '<a class="button-login" href="steamauth/login_steam.php">Log In</a>'; 

      } 
    ?> 

没有回音,PHP会尝试解析您的HTML作为PHP,这是行不通的。

+0

它不会回应整个事情吗? – Garlicvideos

+0

它会回应引号之间的内容。因此,如果是“整个事物”,则表示完全“注销” - 在“if”的情况下链接,而在“else”的情况下表示完整的“登录” - 链接,则是。但这正是你想要的。 – rickdenhaan

0

将您的代码更改为。你必须把html标记出侧PHP

<?php if(isset($_SESSION["steamname"]))   
     { ?> 
       <a class="button-logout" href="steamauth/logout.php">Log Out</a> 
<?php } 
     else 
     { ?> 
       <a class="button-login" href="steamauth/login_steam.php">Log In</a> 

<?php } ?> 

OR

可以呼应html标签

<?php if(isset($_SESSION["steamname"])) 
     { 
      echo '<a class="button-logout" href="steamauth/logout.php">Log Out</a>'; 
     } 
     else 
     { 
      echo '<a class="button-login" href="steamauth/login_steam.php">Log In</a>'; 

     } 
    ?> 
0

如果你不想呼应HTML作为一个字符串,你可以做到这一点像这样的替代语法:

<?php if(isset($_SESSION['steamname'])): ?> 
    <a class="button-logout" href="steamauth/logout.php">Log Out</a> 
<?php else: ?> 
    <a class="button-login" href="steamauth/login_steam.php">Log In</a> 
<?php endif; ?> 
相关问题