0

Very basic question in PHP - I have set a variable to null, why do I get false in the result?

$a = null;
var_dump(isset($a)); // false

How do I just check if a variable is set if isset is not the one I am after?

I can't use empty, for example,

var_dump(empty($b)); // false

But $b is not set at all.

I can do that in javascript which makes more sense,

if (typeof variable !== 'undefined') ...
Run
  • 54,938
  • 169
  • 450
  • 748
  • 1
    null is not really an assignment. Check it out : https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/ – Random Jul 24 '15 at 08:31
  • 3
    @Random `null` is a perfectly fine assignment. – deceze Jul 24 '15 at 08:32
  • @deceze it is not really as it does the same thing as `var $a`, php can't make the difference, so it is more a declaration than an assignment... – Random Jul 24 '15 at 08:34
  • 1
    @Random A variable which exists and holds the value `null` is a perfectly fine variable. When trying to access a non-existent variable, its value will be substituted by `null` and a warning is raised. They're both different though. The default value for a `public $a` in a class also happens to be `null`. Just `$a;` outside a class actually **does nothing at all**, it does not even create the variable, it's a complete NOOP. Still not the same as `$a = null`. – But yes, incidentally it's impossible to distinguish a non-existent variable and a variable holding `null`. – deceze Jul 24 '15 at 08:36
  • @deceze yeah but having an attribute `public $a`, using `$this->a = null` makes nothing... $a is already "declared". – Random Jul 24 '15 at 08:38
  • @Random Well, that assigns `null` to `$this->a` which already happened to contain the value `null` anyway. It *does* something, but nothing much is changing. – deceze Jul 24 '15 at 08:39
  • 1
    @teelou You shouldn't really ever need to check for the existence of variables that much anyway. Initialise your variables properly and keep your fingers off of `isset` most of the time. See [The Definitive Guide To PHP's isset And empty](http://kunststube.net/isset/). – deceze Jul 24 '15 at 08:40
  • @deceze so I can't check it at all in PHP? what about in other languages? – Run Jul 24 '15 at 08:45
  • 2
    Let's keep this discussion to PHP. Talking about all possible languages is not productive, since every one will be different. – deceze Jul 24 '15 at 08:52
  • @teelou there is a full answer here : http://stackoverflow.com/a/418162/4786273 – Random Jul 24 '15 at 09:00
  • @Random I think you mean the next one after that: http://stackoverflow.com/a/2551988/476 – the answer you're pointing to only works for globals, which you're hopefully not dealing with most of the time. – deceze Jul 24 '15 at 09:01
  • @deceze the answer yes, but there are comments on it with attributes and local variables too. But the next answer is interesting too indeed – Random Jul 24 '15 at 09:02
  • @teelou Note that you have exactly the same issue in your Javascript example with `var variable = undefined`... – deceze Jul 24 '15 at 09:03
  • 1
    possible duplicate of [Best way to test for a variable's existence in PHP; isset() is clearly broken](http://stackoverflow.com/questions/418066/best-way-to-test-for-a-variables-existence-in-php-isset-is-clearly-broken) – Random Jul 24 '15 at 09:04

2 Answers2

5

isset checks whether a variable is set and not null. So yes, it will return false if the variable you're checking holds the value null. It is in fact impossible to distinguish a non existent variable from a variable holding null. People will suggest all sorts of hackarounds for this, but none of them work 100% of the time. In PHP you cannot distinguish these two cases, period.

Note that you can use more specialized methods to figure out the difference for array keys or object properties, that's no problem. But plain variables are indistinguishable.

Having said that, you really shouldn't get into a situation where you need this distinction anyway. If you properly initialize your variables, you don't ever need to check whether they're set. You are in full control of all variables in your program, you can absolutely ensure that they exist when they should, so you don't need to check. If PHP then complains about non-existing variables, you know you have a bug which you can fix. All input that you don't control should come in the form of arrays or objects "from outside", which you can inspect in detail and distinguish null from non-existent keys.

See The Definitive Guide To PHP's isset And empty.

deceze
  • 510,633
  • 85
  • 743
  • 889
0

I found a way to get around it but it seems ugly,

$a = null;
$defined_vars = get_defined_vars();
var_dump(array_key_exists('a', $defined_vars)); // true
Run
  • 54,938
  • 169
  • 450
  • 748
  • 1
    Note that this will fail for certain special cases, like `$_POST` inside a function scope. As I said, there is no 100% working workaround. You really need to ask yourself: why are you checking for the existence of a variable in the first place?! And the answer typically is: if you need to, you're doing it wrong and must initialise your variables properly instead. – deceze Jul 24 '15 at 08:58
  • You can just check is_null() because that will explicitly check for the „null“ –  Apr 24 '22 at 10:19