Check if variable is_undefined in PHP

tjbourke picture tjbourke · Jun 7, 2013 · Viewed 33.7k times · Source

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.

Answer

cljk picture cljk · Jun 7, 2013

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.