With error reporting on, or even for best practice, when unsetting a variable in PHP, should you check to see if it exist first (in this case it does not always exist) and the unset it, or just unset it?
<?PHP if (isset($_SESSION['signup_errors'])){ unset($_SESSION['signup_errors']); } // OR unset($_SESSION['signup_errors']); ?>
PHP isset() Function The isset() function checks whether a variable is set, which means that it has to be declared and is not NULL. This function returns true if the variable exists and is not NULL, otherwise it returns false.
You can use the PHP isset() function to test whether a variable is set or not. The isset() will return FALSE if testing a variable that has been set to NULL.
The Scope Resolution Operator (also called Paamayim Nekudotayim) or in simpler terms, the double colon, is a token that allows access to static, constant, and overridden properties or methods of a class.
The unset() function in PHP resets any variable. If unset() is called inside a user-defined function, it unsets the local variables. If a user wants to unset the global variable inside the function, then he/she has to use $GLOBALS array to do so. The unset() function has no return value.
Just unset it, if it doesn't exist, nothing will be done.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With