Check if variable is_undefined in PHP
Asked Answered
L

7

15

In PHP, I want to check if a variable has not been set/defined, where setting a variable NULL is considered set/defined.

I'm aware everything here: http://php.net/manual/en/types.comparisons.php including isset(), empty(), and is_null(). None of these appear to be what I'm looking for. Consider the following example:

<?php 
$myNull = null;
echo 'isset($myNull): "'.isset($myNull).'"<br />';
echo '$myNull value = "'.$myNull . '"<br />';

echo "<br />";

echo 'isset($myUndefined): "'.isset($myUndefined).'"<br />';
echo '$myUndefined value = "'.$myUndefined . '"<br />';
?>

This example outputs something like:
isset($myNull): ""
$myNull value = ""

isset($myUndefined): ""
Notice: Undefined variable: myUndefined in C:\wamp\www\plm\temp4.php on line 9
$myUndefined value = ""

I want to know if a variable is Undefined as it says above in the notice. I want a function, call it "is_undefined", where

$myNull = null;
is_undefined($myNull); // is false
is_undefined($myUndefined); // is true

Anyone? Thanks in advance.

Longtin answered 7/6, 2013 at 21:11 Comment(0)
E
18

I haven´t used it yet - but I think that "get_defined_vars" should be worth a look... http://php.net/manual/en/function.get-defined-vars.php

I would give it a try and dump the result.

Epiphysis answered 7/6, 2013 at 21:20 Comment(2)
Also, if you are within the global scope you can check if array_key_exists in $GLOBALS.Circumspect
I was doubtful there was a solution out there. This works great though, thanks! I used it in conjunction with array_key_exists($key, $array).Longtin
P
10

I think that get_defined_vars is a good candidate for such job:

array_key_exists('myNull', get_defined_vars());

Should do what you expect.

If you work on a global context, you can also use:

array_key_exists('myNull', $GLOBALS);
Plowman answered 7/6, 2013 at 21:22 Comment(0)
S
8

If you want an is_undefined function I would prefer not to work with arrays so I would do this:

function is_undefined(&$test) {
    return isset($test) && !is_null($test);
}

So when you echo isset($myNull); it converts the boolean(true) to "". thats why the value is blank. If you want to see it on the screen you can do var_dump(isset($myNull)); that will display if it's true or false.

Also you have an echo of $myUndefined but it's not set yet so that's why you get a warning. What you want to do is:

if (!empty($myUndefined)) {
    // variable is defined so do something with it
    echo '$myUndefined value = "' . $myUndefined . '"<br />';
} else {
    echo 'Oops, $myUndefined is Undefined!<br />";
}

Here is a brief overview of isset() vs. is_null() vs. empty()

$foo = null;
// isset($foo) == true;
// empty($foo) == true;
// is_null($foo) == true;

// Notice I don't set $foo2 to anything
// isset($foo2) == false;
// empty($foo2) == true;
// is_null($foo2) throws a notice!

$foo3 = false;
// isset($foo2) == true;
// empty($foo2) == true;
// is_null($foo2) == false;

$foo4 = 1234;
// isset($foo2) == true;
// empty($foo2) == false;
// is_null($foo2) == false;
Susannsusanna answered 7/6, 2013 at 21:17 Comment(10)
is_null() will cause E_NOTICE if the variable is undefined.Previse
empty() returns true if it is a defined NULL variable :)Previse
I know this is OCD, but your closing quote in the second string should be a ' not a "... the red semicolon and close bracket is driving me nuts, and I can't change it because it's a single character edit.Gotama
Haha @JasonNichols I edited my post about 30 seconds before you commented. I hate that too!Susannsusanna
@GGio using empty() on a not set variable will NOT give you a notice. Trust me I checked ;)Susannsusanna
@GGio my entire code: <?php error_reporting(E_ALL); empty($test); Result? No notices! So either you're understanding php.net incorrectly or there's a typo on their site. Trust me. Try it your selfSusannsusanna
@GGio... haha... oops? amiright? hehe delete your comments so you don't look silly :)Susannsusanna
+1 Very nice solution where you can use the real variable instead of just the name as a string (the get_defined_vars() solutions...).Devaughn
You missed the point @chrislondon, and you didn't read my post very well. I said I understand all those functions and none of them work. I said is_undefined() should return false if set NULL, and true if never set. A simple test would show your function returns false and false.Longtin
This is_undefined implementation is improperly returning false for variables set to null and yields the exact same results as isset for defined, undefined, and variables set to null. The && !is_null($test) part is completely redundant.Mailman
O
4

You can use compact() for this too, if the variable you give it isn't in the symbol table it returns an empty array, otherwise an array containing the variable name/value pair, just cast the result to a boolean

<?php

$myNull = null;

$isDefined = (bool) compact('myNull'); // true

$otherIsDefined = (bool) compact('myUndefined'); // false 
Oft answered 7/6, 2013 at 22:38 Comment(0)
A
0

If you are using OOP then use overloading __isset() this function will execute when you are trying to access a variable that is not defined anywhere. example:

 public function __isset($name) {
    echo 'Hello';
 }

Thus, will avoid any error message or notice related to undefined variable

Aristocracy answered 7/6, 2013 at 21:22 Comment(2)
I like the idea, but take care because property_exists will not call your magic method: this can be confusing in some ways.Plowman
I am using OOP, but I want more than just to kill the notice, I want to check if a variable has been set ($asdf = "anything", or $asdf = null, or etc). This may still work in some way, but I prefer the get_defined_vars() functions. Thanks anyway.Longtin
D
0

Yes, just like Mr. Jonathan mentioned above, we could use array_key_exists() + $GLOBALS rather than get_defined_vars() to identify Undefined variable to null

$x;  // $x is undefined 
$y=null;  // $y is defined and is NULL type variable with the only null value
$z=[];    // $z is an array object

if( array_key_exists('x', $GLOBALS) && is_null($x) ) echo "\$x exists and is null\n";
if( array_key_exists('y', $GLOBALS) && is_null($y) ) echo "\$y exists and is null\n";
if( array_key_exists('z', $GLOBALS) && is_null($z) ) echo "\$he exists and is null\n";

// output 
$y exists and is null
Dawna answered 30/12, 2013 at 3:54 Comment(0)
R
0

To check a variable is define and have some value we can use isset() in a condition

if(!isset($var)){}
Rip answered 2/2, 2021 at 9:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.