-3

I have some PHP that displays the users session name in the HTML and if the user is not logged in, then I would like it to display "User" instead.

Can I have an if statement in this case? I tried it myself but I got header errors.

This is what I've tried so far but each practical attempt just spits out more errors at me.

This is a different approach I have tried.

<?=$_SESSION['sess_user'] or ("User");?>!
  • If you had "headers already sent" errors at any point, it points to other problems, which [this question thoroughly explains](http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) Fix them now before they cause you bigger headaches later! :) – Michael Berkowski May 13 '14 at 02:04
  • learn about `if else` statements. Then learn about `ternary` statements. The answer from MyHunter is an example of a ternary statement – arserbin3 May 13 '14 at 02:04
  • I've done if else statements before, but I didn't think I could put them into the HTML itself. I will do further research, thank you! – user3464627 May 13 '14 at 02:07
  • @MichaelBerkowski Yes they are a headache! a couple of white spaces gave me a headache for hours this morning – user3464627 May 13 '14 at 02:09

1 Answers1

2
<?php echo (isset($_SESSION['sess_user']) ? $_SESSION['sess_user'] : "User"); ?>

This will check if the session is set, if it is then echo the session, if it isn't then it will echo "User".

MrHunter
  • 1,892
  • 1
  • 15
  • 23