3

After 10 years of PHP programming, just now I found out that isset() is not good at all, according to PHP isset() manual, isset() returns false when variable 'is set' and has a null value.

Well, I was working with an array and I fixed this by array_key_exists(), just after about 2 hours of reading and testing codes.

but what to use for variables?

isset($var) || is_null($var)

This makes NOTICE when $var is not set... empty() is completely in another world too. As I said, is_null() makes NOTICE on not set variables and returns true!!... Well, this one is out, too.

user5483434
  • 492
  • 7
  • 17
  • 4
    `isset($var) && !is_null($var)`. In your example, if `$var` isn't set, the `isset` will return `false` causing the `is_null` to be executed (throwing the notice). By using `&&`, if `isset` returns `false`, the expression will return false immediately. – Jonnix Dec 21 '15 at 14:38
  • You are right, looks like I need to sleep :)... Well, I'm looking for a better solution... In large apps we check for variables existence hundreds or thousands of times, isn't there a better solution than `isset($var) && !is_null($var)`... Also, it is not possible to define a function for this purpose. Because PHP shows errors if variable is not set, and sets null to variable if we pass by reference... Ah PHP! – user5483434 Dec 21 '15 at 14:45
  • @FirstOne empty() returns true on null, false, "" (empty string), 0 (integer zero), 0.0 (float zero), array() or [] (empty array), $var (variable declared but has no value), AND "0" !!! (an string containing zero character). Actually, it should not be used for checking for variable existence at all. – user5483434 Dec 21 '15 at 14:50
  • You forgot to describe what you want to achieve. [`isset($var)` is the same as `! is_null($var)`](http://php.net/manual/en/types.comparisons.php) (except for a notice thrown by `is_null()` when the variable does not exist). This means `isset($var) && ! is_null($var)` is the same as `isset($var)`. – axiac Dec 21 '15 at 19:08

1 Answers1

0

A quick guide on is_null and empty can be found here, I used to reference it quite a bit.

As you noted, is_null is a complainer. The only way to test this without throwing a NOTICE is to check if the variable is set in the global vars array. You can do this by testing as such:

// Is Set (to anything)
if(!(isset($var) || array_key_exists('var',get_defined_vars()))){ /* ... */ }
// Set and Null
if(!(is_null($var) || array_key_exists('var',get_defined_vars())))) { /* ... */ }

// Etc

The problem here is that you are using get_defined_vars and this has significant performance impacts, both in wall-time and memory usage, if over-used, and I would suggest it should just be used for debugging.

If you are interested in strictly global variables, you can use the $GLOBALS supervar. However, this will not give you access to variables inside the scope of a function or method: https://stackoverflow.com/a/418162/1301994

If you are trying to avoid NOTICES to increase performance, this is not worth it, as the overhead for check is greater than the slight performance gains.

I guess it really matters what your goals are. If you are working in an OOP fashion, then you should be able to tell fairly clearly if a variable is set or not in the scope of the method. Otherwise, it would be best to not code in such a way that undefined and NULL behave differently. If this is not possible, consider utilizing other flags to help you get around the use of get_undefined_vars.

Check below for a little more on the topic:

Is var set to null same as undefined and how to check differences

Community
  • 1
  • 1
Mike
  • 1,968
  • 18
  • 35
  • This misses the case where the variable is a global. The correct condition is `(array_key_exists('var',get_defined_vars()) or array_key_exists('var',$GLOBALS)` – Pedro Gimeno May 24 '21 at 22:59