1

I just came across a strange situation. When I try the following code in $ php -a, I receive an error:

php > var_dump(isset(null));

PHP Fatal error: Cannot use isset() on the result of an expression (you can use "null !== expression" instead) in php shell code on line 1

But when I do the same thing with empty(), everything is ok:

php > var_dump(empty(null));
bool(true)

Can anyone explain why I receive an error when I try isset(null)?


Update

Thank you all for your answers. I asked this question just to make sense of why isset() is behaving differently from empty().

To me, both of them are php functions and both accept a parameter. So, as any other function in php, calling isset(null) should be a valid statement. Aren't we passing null as a value to isset() function? So why php consider it as an expression?

Armin Sam
  • 903
  • 9
  • 23
  • 1
    Probably because null is/can be empty and isset checks if something is set and needs to compare with something; that's my take on it. Here, have a look at this Q&A http://stackoverflow.com/questions/8236354/php-is-null-or-empty also http://stackoverflow.com/questions/5615747/what-is-the-difference-between-null-and-empty – Funk Forty Niner Nov 01 '16 at 16:00
  • If you do `isset(null);` you get this error - but what's the point anyway of doing that anyway? If you do `$var = null; var_dump(isset($var));` instead, that would be valid and produce `bool(false)`, because then you're dealing with a *variable* and not an expression (see the manual: http://php.net/manual/en/function.isset.php). – Qirel Nov 01 '16 at 16:02
  • Obviously, nobody "answered" the question. *(so far).* I need no upvoted comments or answer accepted. – Funk Forty Niner Nov 01 '16 at 16:20
  • This shold be fairly obvious if you read the PHP documentation. – junkfoodjunkie Nov 01 '16 at 16:21

2 Answers2

4

Testing if an expression is "set" doesn't make sense. As per the manual, isset is used to

Determine if a variable is set and is not NULL.

If you want to check if an expression is null, use is_null, or as the error message suggests, null !== expression.

The manual for empty suggests something similar:

Determine whether a variable is considered to be empty.

until you read slightly further down, in the changelog:

5.5.0 empty() now supports expressions, rather than only variables.

Prior to this, empty(null) would have thrown an error along the lines of

Parse error: syntax error, unexpected ')', expecting :: (T_PAAMAYIM_NEKUDOTAYIM) in ... on line ...

iainn
  • 16,826
  • 9
  • 33
  • 40
  • The [manual](http://php.net/manual/en/function.empty.php) says the same thing about empty: "Determine whether a **variable** is considered to be empty.". But it seems empty() accepts expressions while isset() does not. – Armin Sam Nov 01 '16 at 19:04
  • @ArminSam So it does, I've just realised that the important part of the question is the ambiguity between the two. I've updated my answer to clarify. – iainn Nov 01 '16 at 19:55
  • But why would php consider null, or any other value for that matter, as expression? We are calling a function called `isset` and passing parameter `null` to it. What's wrong with that? – Armin Sam Nov 01 '16 at 20:03
  • 1
    @ArminSam - an expression means something that *produces* the value, after it's evaluated - `null`, constants, functions - they are all some sort of an expression which ultimately produces something. A `null` is a special **value**, meaning that it's an expression and not a *variable*. `isset` expects a variable, while `empty` can work with expressions. You can't check if an expression is set, it doesn't make much sense to do so because - well, it's set since its value is present. You check whether **variables** are set. – N.B. Nov 01 '16 at 20:20
  • @N.B. Thanks for the explanation. I also found why isset is not behaving like a normal function, ... because [**it's not a function**](http://php.net/manual/en/functions.variable-functions.php)! It's a language construct like `echo`, `die`, and so on. – Armin Sam Nov 01 '16 at 20:39
1

Ok, I found something that I didn't know before in php which could be the answer to this question. According to the manual, isset() is not a function, but rather a language construct like if ... else, foreach and while:

Note: Because this [referring to isset()] is a language construct and not a function, it cannot be called using variable functions.

There are a few more of these language constructs that can be easily confused with functions, including:

  • unset()
  • empty()
  • die()
  • include()

So now it makes sense why isset(null) doesn't work. We are trying to use a construct that expects a variable inside the parenthesis. Providing anything else other than a variable will result in syntax error during parsing of the code.

Armin Sam
  • 903
  • 9
  • 23