Reputation: 41
I need help with isset() function. I do not know what its return value is. Is it better to write?
if (!isset($_SESSION['user'])) {
header("Location: login.php");
}
or
if (isset($_SESSION['user']) != "") {
header("Location: login.php");
}
Please help
Upvotes: 0
Views: 37
Reputation: 486
Your question sounds silly but it is actually the real issue to me. For over 10 years with PHP, I always want to find a want to shorten my code when isset function involved. And I have my solution for this isset
issue. For your particular case, it should be:
Using empty
:
if (empty($_SESSION['user'])) { header("Location: login.php"); }
Upvotes: 1
Reputation: 2499
If you want to check for both you can have:
if (!isset($_SESSION['user']) && $_SESSION['user'] != "") {
header("Location: login.php");
}
Upvotes: 1
Reputation: 21661
Its a Boolean ( true/false )
if (!isset($_SESSION['user'])) {
header("Location: login.php");
}
http://php.net/manual/en/function.isset.php
Upvotes: 0