hypergalax Posted April 12, 2009 Share Posted April 12, 2009 Hi, i am having a problem with an if statement. I wish to print two different texts depending if the users session is true or false , this is my current code. <?php if (isset($_SESSION[true])) print "First message for those logged in" else print "Second message for those not logged in" ?> the error i receive is Parse error: parse error in E:\wamp\www\index.php on line 23 Any help would be greatly appreciated Link to comment https://forums.phpfreaks.com/topic/153740-if-else-statement-hypergalax/ Share on other sites More sharing options...
MasterACE14 Posted April 12, 2009 Share Posted April 12, 2009 try... <?php if (isset($_SESSION['true'])) { echo "First message for those logged in"; } else { echo "Second message for those not logged in"; } ?> Link to comment https://forums.phpfreaks.com/topic/153740-if-else-statement-hypergalax/#findComment-807912 Share on other sites More sharing options...
hypergalax Posted April 12, 2009 Author Share Posted April 12, 2009 try... <?php if (isset($_SESSION['true'])) { echo "First message for those logged in"; } else { echo "Second message for those not logged in"; } ?> Thanks that got rid of the error, however it seems to only print the second statement even if i am logged in or out, i tried changing true to false to see what happens but it still will only print the second statement. Link to comment https://forums.phpfreaks.com/topic/153740-if-else-statement-hypergalax/#findComment-807913 Share on other sites More sharing options...
Axeia Posted April 12, 2009 Share Posted April 12, 2009 If you don't wish to use { } for single line statements then they got to connect like this: <?php if (isset($_SESSION[true])) print "First message for those logged in"; else print "Second message for those not logged in"; ?> But in general, the way the above poster did it is a lot more clear. (and don't forget the semicolumns at the end) Link to comment https://forums.phpfreaks.com/topic/153740-if-else-statement-hypergalax/#findComment-807914 Share on other sites More sharing options...
MasterACE14 Posted April 12, 2009 Share Posted April 12, 2009 think you need to do abit of research into how sessions work. When you log in you need to set a session variable like... $_SESSION['userid'] = 1; then you can see if it is set. <?php if(isset($_SESSION['userid'])) { echo "session has started!"; } else { echo "no session present!"; } ?> look into sessions in the php manual www.php.net @ Axeia - you would never have the 'else' returned with that code. Link to comment https://forums.phpfreaks.com/topic/153740-if-else-statement-hypergalax/#findComment-807915 Share on other sites More sharing options...
hypergalax Posted April 12, 2009 Author Share Posted April 12, 2009 Thanks for sharp replies and also thanks to you MasterACE14, the last script was very helpful and i have now managed to get the script to work, it was a silly mistake of not setting the variable :>. Thanks agian. Link to comment https://forums.phpfreaks.com/topic/153740-if-else-statement-hypergalax/#findComment-807918 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.