Ok Ram
Ok Ram

Reputation: 41

php what's better to use in isset() functions

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

Answers (3)

Trac Nguyen
Trac Nguyen

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

mrateb
mrateb

Reputation: 2499

If you want to check for both you can have:

if (!isset($_SESSION['user']) && $_SESSION['user'] != "") {
   header("Location: login.php");
}

Upvotes: 1

ArtisticPhoenix
ArtisticPhoenix

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

Related Questions