Jump to content

If Else statement - Hypergalax


hypergalax

Recommended Posts

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

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.

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)

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.