Search code examples
phpprogramming-languages

In PHP, if a variable does not have a value, why does isset() still return TRUE?


Here is my code:

<?php
   $ja = ''; 

  if(isset($ja))
   echo "cool!";

?>

I get a "cool!" when running this simple piece of code in my browser. I learned from php.net that

isset — Determine if a variable is set and is not NULL

Well, in my code, I did declare the variable $ja, but I didn't add any value to it, so shouldn't it be "NULL"?


Solution

  • Even though '' seems like nothing, it still has a value (a NULL character at the end of the string).

    isset() checks if the variable is set or not, which in the case (to ''), it is. You may want to set $ja to NULL first beforehand, instead of setting it to an empty string... or use empty() ;)