检查会话 - php不打印[关闭]
问题描述:
<?php session_start();
$_SESSION['loggedIn'] = "no";
echo "Logged In: ". $_SESSION['loggedIn'];
if($_SESSION['loggedIn']=="yes"):
echo "logged in";
}
else{
echo "not logged in";
}
?>
I am simply trying to check if the session "loggedIn" is yes or no, but the program doesn't print anything.
Why does it not print anything?
答
You are using alternative if/then as well as regular syntax.
if($_SESSION['loggedIn']=="yes"):
should be
if($_SESSION['loggedIn']=="yes"){
echo "logged in";
}
else{
echo "not logged in";
}
its a simple syntax error
答
Remove :
after if
and place {
Change
if($_SESSION['loggedIn']=="yes"):
To
if($_SESSION['loggedIn']=="yes") {
答
You've used a colon to open an if:
if($_SESSION['loggedIn']=="yes"):
Change it to a {
if($_SESSION['loggedIn']=="yes"){
It is valid syntax to open an if with :
but you must close it with endif;
instead of }
.
Example of the alternate syntax:
if($_SESSION['loggedIn']=="yes"):
echo "logged in";
else:
echo "not logged in";
endif;
答
either remove the :
or use the alternative syntax php alternative syntax for control structures
either:
if($_SESSION['loggedIn']=="yes"){
echo "logged in";
}
else{
echo "not logged in";
}
or:
if($_SESSION['loggedIn']=="yes"):
echo "logged in";
else:
echo "not logged in";
endif;