Check if var exist before unsetting in PHP?

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!


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

Tags:

Php