Hi. I'm confusing. Can somebody help me, please. Sometimes this snippet works: if(!isset($_SESSION['login'])) { Sometimes this works: if(isset($_SESSION['login'])) { Which one choose I use? Thanks
if(!isset($_SESSION['login'])) { = determines if the $_SESSION is NOT set. if(isset($_SESSION['login'])) { = determines if the $_SESSION IS set. Example: <?php if(isset($_SESSION['login'])) { echo "You are logged in!"; } else { echo "You are not logged in!"; } ?> PHP: ... <?php if(!isset($_SESSION['login'])) { echo "You are not logged in!"; } else { echo "You are logged in!"; } ?> PHP: So you could say its in reverse order - but both can be used to achieve the same thing.
The choice of which to use is based on what you want the next lines after the conditional to do. Look closely at dan's example above.
actually, if (!isset($_SESSION['login'])) technically doesn't really mean "if SESSION['login'] is NOT set"...in fact it's more correct to translate it as, "if the expression isset($_SESSION['login']) returns false". you can drive yourself crazy with comparisons if you misinterpret the actual meaning of an expression.
!isset is looking for FALSE to be returned from the function, whereas the other way will check for TRUE.
isset will return TRUE if the checked variable is not NULL. Meaning that isset(false) would still return true. empty however returns TRUE if the checked variable is NULL, FALSE, '' (empty string) or an empty array. empty is also implicitly used in if statement.
isset() means the variable exists whether it has a value in it or not. !isset() means the variable doesn't exist yet. Most times, better to use empty().
! is the NOT operator, it inverses the value of a boolean expression if isset were to return true, !isset would not be true if isset were to be false, !isset would be true