To check if the variable is set in PHP, use the PHP isset() function.
PHP unset
PHP unset() is a built-in function that terminates the specified variables. Only a local variable is destroyed if the globalized variable is unset() inside the function.
The variable in the calling context will retain the same value as before unset() was called. If the variable PASSED BY REFERENCE is unset() inside a function, only the local variable is destroyed.
Syntax
The syntax of PHP unset() function is following.
unset (var1, var2.... )
Parameters
You can pass as many variables as possible.
If we want to unset() the global variable inside of the function, then use the $GLOBALS array to do so:
unset($GLOBALS[var]);
See the following code.
<?php // app.php $el = 'Millie Bobby Brown'; unset($el); echo $el;
See the output.
➜ pro php app.php PHP Notice: Undefined variable: el in /Users/krunal/Desktop/code/php/pro/app.php on line 5 Notice: Undefined variable: el in /Users/krunal/Desktop/code/php/pro/app.php on line 5 ➜ pro
If the static variable is unset() inside the function, unset() destroys the variable only in the context of the rest of the function.
The following calls will restore a previous value of the variable.
<?php // app.php function eleven() { static $el; $el++; echo "Before unset: $el, "; unset($el); $el = 11; echo "after unset: $el\n"; } eleven(); eleven(); eleven();
See the output.
➜ pro php app.php Before unset: 1, after unset: 11 Before unset: 2, after unset: 11 Before unset: 3, after unset: 11 ➜ pro
PHP unset array
See the following code.
<?php // app.php $data = ['Netflix', 'Disney+', 'Comcast', 'HBO GO']; print_r($data); unset($data); print_r($data);
See the output.
➜ pro php app.php Array ( [0] => Netflix [1] => Disney+ [2] => Comcast [3] => HBO GO ) PHP Notice: Undefined variable: data in /Users/krunal/Desktop/code/php/pro/app.php on line 7 Notice: Undefined variable: data in /Users/krunal/Desktop/code/php/pro/app.php on line 7 ➜ pro
Using unset() casting
The unset() casting is often confused with the unset() function.
For completeness, the (unset) casting serves only as the NULL-type cast. It does not alter a variable; it’s casting. The (unset) cast is deprecated as of PHP version 7.2.0.
<?php $name = 'AppDividend'; var_dump((unset) $name); var_dump($name);
See the following output.
➜ pro php app.php PHP Deprecated: The (unset) cast is deprecated in /Users/krunal/Desktop/code/php/pro/app.php on line 5 Deprecated: The (unset) cast is deprecated in /Users/krunal/Desktop/code/php/pro/app.php on line 5 NULL string(11) "AppDividend" ➜ pro
PHP unset Cookies
Use the superglobal _COOKIE variable. See the code.
unset($_COOKIE['mycookiename']);
PHP unset session
A session creates the file in the temporary directory on the server where registered session variables and their values are stored.
The data will be available on all pages on the site during that visit.
If you want to destroy the single session variable, you can use the unset() function to unset a session variable. See the code.
<?php unset($_SESSION['data']);
That’s it for this tutorial.