Checking if a $_COOKIE value is empty or not

sunjie picture sunjie · Jun 22, 2011 · Viewed 20.2k times · Source

I assign a cookie to a variable:

$user_cookie = $_COOKIE["user"];

How can I check if the $user_cookie received some value or not?

Should I use if (empty($user_cookie)) or something else?

Answer

LainIwakura picture LainIwakura · Jun 22, 2011

These are the things empty will return true for:

  • "" (empty string)
  • 0 (0 as an integer)
  • 0.0 (0 as float)
  • "0" (0 as string)
  • NULL
  • FALSE
  • array() (an empty array)
  • var $var; (a declared variable not in a class)

Taken straight from the php manual

So to answer your question, yes, empty() will be a perfectly acceptable function, and in this instance I'd prefer it over isset()