AppDividend
Latest Code Tutorials

PHP unset Example | unset() Function In PHP Tutorial

0

PHP unset Example | unset() Function In PHP Tutorial is today’s topic. The unset() terminates the specified variables. If the globalized variable is unset() inside of the function, only a local variable is destroyed. The variable in the calling context will retain the same value as before unset() was called. If the variable that is PASSED BY REFERENCE is unset() inside of a function, only the local variable is destroyed.

PHP unset Example

If we want to check if the variable is set or not then you can use the PHP isset() function.

The syntax of PHP unset() function is following.

unset (var1, var2.... )

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 of the function, unset() destroys the variable only in the context of the rest of the function.

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.

The (unset) casting serves only as the NULL-type cast, for completeness. 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 to all pages on the site during that visit.

If you want to destroy the single session variable, then you can use the unset() function to unset a session variable. See the code.

<?php
   unset($_SESSION['data']);

Finally, PHP unset Example | unset() Function In PHP Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.