-1

Is there a way to add a button for a if/else statement? This is my code

<?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>

                }
        ?> 

But my server keeps saying that it's a invalid. My understanding of php isn't that great but what I'm trying to do is to make it so that if a user is logged in a logout button will appear and if not it will be login. My current method doesn't work so is it even possible? Thanks.

P.S. I've tried echoing it out, no luck either. P.S.S I don't think it has anything to do with my isset command. I did a plain echo and it worked out fine.

  • You do need to echo the button out. Why didn't that work for you? – rickdenhaan Aug 06 '17 at 10:48
  • I don't know, when I do that, it just says "unexpected "<"" and so on down the line until my entire command line disappears. It might be my formatting. Do you have a code I could try? –  Aug 06 '17 at 10:51
  • 2
    Possible duplicate of [PHP Parse/Syntax Errors; and How to solve them?](https://stackoverflow.com/questions/18050071/php-parse-syntax-errors-and-how-to-solve-them) – user3942918 Aug 06 '17 at 10:53
  • Possible duplicate of [How can I echo HTML in PHP?](https://stackoverflow.com/questions/1100354/how-can-i-echo-html-in-php) – mickmackusa Aug 07 '17 at 09:02

3 Answers3

3

You need to echo the HTML you want:

<?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>';

            }
    ?> 

Without the echo, PHP will try to parse your HTML as PHP, which won't work.

rickdenhaan
  • 10,857
  • 28
  • 37
  • Won't it echo the entire thing out? –  Aug 06 '17 at 10:57
  • It will echo what's between the quotation marks. So if by "entire thing" you mean the full "Log Out"-link in the case of `if`, and the full "Log In"-link in the case of `else`, then yes. But that's exactly what you want. – rickdenhaan Aug 06 '17 at 10:58
0

change your code to. You have to put html tags out side 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

You can echo html tags

<?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>';

         }
    ?> 
B. Desai
  • 16,414
  • 5
  • 26
  • 47
0

If you don't want to echo html as a string, you can do it like this with alternative syntax:

<?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; ?>
Sasa Blagojevic
  • 2,110
  • 17
  • 22