Check if var exist before unsetting in PHP?

Viewed 32002

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']);
?>
5 Answers

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.

Related