This question already has an answer here:
I am using the $_SESSION global variable on each page on my application so that I can control access to my site with login functionality. Instead of using the following path that searches for TRUE else FALSE, as showed in the example below, how can you reverse the condition?
<?php
if (isset($_SESSION['id'])) {
echo $_SESSION['id'];
} else {
echo "You are not logged in!";
}
?>
</div>
Look at logical operators in the PHP manual, your problem is solved and explained very well there. So please, take a look.
If you want to turn your if
statement around, you should do this:
<?php
if (!isset($_SESSION['id'])) {
echo "You are not logged in!";
} else {
echo $_SESSION['id'];
}
?>
Simply put: !
means not.
In this particular case, you are using the !
operator, which means that you are turning around your if
statement normally goes through if it's true. So, if it's isset()
but you add the !
operator, you check if it's notisset()
, in a way, where the "not" is a !
.
The manual puts it like this: if(!$a){}
is true if $a
is not true.
I think you're looking for this? You just have to negate isset with !, so you're essentially saying IF NOT / ELSE instead of IF / ELSE
<?php
if ( !isset( $_SESSION['id'] ) ) {
echo "You are not logged in!";
} else {
echo $_SESSION['id'];
}
An exclamation sign !
equals "not", like
if (!isset(...etc...