Check if var exist before unsetting in PHP?

Solution 1:

Just unset it, if it doesn't exist, nothing will be done.

Solution 2:

From the PHP Manual:

In regard to some confusion earlier in these notes about what causes unset() to trigger notices when unsetting variables that don't exist....

Unsetting variables that don't exist, as in

<?php
unset($undefinedVariable);
?>

does not trigger an "Undefined variable" notice. But

<?php
unset($undefinedArray[$undefinedKey]);
?>

triggers two notices, because this code is for unsetting an element of an array; neither $undefinedArray nor $undefinedKey are themselves being unset, they're merely being used to locate what should be unset. After all, if they did exist, you'd still expect them to both be around afterwards. You would NOT want your entire array to disappear just because you unset() one of its elements!

Solution 3:

Using unset on an undefined variable will not cause any errors (unless the variable is the index of an array (or object) that doesn't exist).

Therefore the only thing you need to consider, is what is most efficient. It is more efficient to not test with 'isset', as my test will show.

Test:

function A()
{
    for ($i = 0; $i < 10000000; $i++)
    {
        $defined = 1;
        unset($defined);
    }
}

function B()
{
    for ($i = 0; $i < 10000000; $i++)
    {
        $defined = 1;
        unset($undefined);
    }
}

function C()
{
    for ($i = 0; $i < 10000000; $i++)
    {
        $defined = 1;
        if (isset($defined))
            unset($defined);
    }
}

function D()
{
    for ($i = 0; $i < 10000000; $i++)
    {
        $defined = 1;
        if (isset($undefined))
            unset($undefined);
    }
}

$time_pre = microtime(true);
A();
$time_post = microtime(true);
$exec_time = $time_post - $time_pre;
echo "Function A time = $exec_time ";

$time_pre = microtime(true);
B();
$time_post = microtime(true);
$exec_time = $time_post - $time_pre;
echo "Function B time = $exec_time ";

$time_pre = microtime(true);
C();
$time_post = microtime(true);
$exec_time = $time_post - $time_pre;
echo "Function C time = $exec_time ";

$time_pre = microtime(true);
D();
$time_post = microtime(true);
$exec_time = $time_post - $time_pre;
echo "Function D time = $exec_time";
exit();

Results:

  1. Function A time = 1.0307259559631
    • Defined without isset
  2. Function B time = 0.72514510154724
    • Undefined without isset
  3. Function C time = 1.3804969787598
    • Defined using isset
  4. Function D time = 0.86475610733032
    • Undefined using isset

Conclusion:

It is always less efficient to use isset, not to mention the small amount of extra time it takes to write. It's quicker to attempt to unset an undefined variable than to check if it can be unset.

Solution 4:

If you would like to unset a variable then you can just use unset

unset($any_variable); // bool, object, int, string etc

Checking for its existence has no benefit when trying to unset a variable.

If the variable is an array and you wish to unset an element you must make sure the parent exists first, this goes for object properties too.

unset($undefined_array['undefined_element_key']); // error - Undefined variable: undefined_array

unset($undefined_object->undefined_prop_name); // error - Undefined variable: undefined_object

This is easily solved by wrapping the unset in an if(isset($var)){ ... } block.

if(isset($undefined_array)){
    unset($undefined_array['undefined_element_key']); 
}

if(isset($undefined_object)){
    unset($undefined_object->undefined_prop_name); 
}

The reason we only check the variable(parent) is simply because we don't need to check the property/element and doing so would be a lot slower to write and compute as it would add an extra check.

if(isset($array)){
...
}

if(isset($object)){
...
}

.vs

$object->prop_name = null;
$array['element_key'] = null;

// This way elements/properties with the value of `null` can still be unset.

if(isset($array) && array_key_exists('element_key', $array)){
...
}

if(isset($object) && property_exists($object, 'prop_name')){
...
}

// or 

// This way elements/properties with `null` values wont be unset.

if(isset($array) && $array['element_key'])){
...
}

if(isset($object) && $object->prop_name)){
...
}

This goes without saying but it is also crucial that you know the type of the variable when getting, setting and unsetting an element or property; using the wrong syntax will throw an error.

Its the same when trying to unset the value of a multidimensional array or object. You must make sure the parent key/name exists.

if(isset($variable['undefined_key'])){
    unset($variable['undefined_key']['another_undefined_key']);
}

if(isset($variable->undefined_prop)){
    unset($variable->undefined_prop->another_undefined_prop);
}

When dealing with objects there is another thing to think about, and thats visibility.

Just because it exists doesn't mean you have permission to modify it.

Solution 5:

Check this link https://3v4l.org/hPAto

The online tool shows code compatibility for different versions of PHP

According to this tool the code

unset($_SESSION['signup_errors']);

would work for PHP >=5.4.0 without giving you any notices/warnings/errors.