PHP unset () Destroy single or multiple variables _php tutorial

Source: Internet
Author: User
The unset function is a function of PHP's own destruction variables, we introduced the use of unset to destroy the static variables and global variables, and can destroy the array variable Oh, let's look at the example below.

Example 1. Unset () example

The code is as follows Copy Code

Destroying a single variable
Unset ($foo);

Destroying a single array element
unset ($bar [' Quux ']);

Destroy more than one variable
Unset ($foo 1, $foo 2, $foo 3);
?>

The behavior of unset () in a function depends on the type of variable you want to destroy.

If you unset () a global variable in a function, only the local variable is destroyed, and the variable in the calling environment keeps the same value as before the call to Unset ().

The code is as follows Copy Code

function Destroy_foo () {
Global $foo;
Unset ($foo);
}

$foo = ' Bar ';
Destroy_foo ();
Echo $foo;
?>

The above example will output:

Bar

If you unset () a variable passed by reference in a function, only the local variable is destroyed, and the variable in the calling environment keeps the same value as before the call to Unset ().

The code is as follows Copy Code

function foo (& $bar) {
Unset ($bar);
$bar = "blah";
}

$bar = ' something ';
echo "$barn";

Foo ($bar);
echo "$barn";
?>
The above example will output:

Something
Something

unset () static variable
Strictly speaking, destroying a static variable with unset () only breaks the reference between the variable name and the value of the variable.

Example:

The code is as follows Copy Code

function foo () {
static $b;
$a + +;
$b + +;
echo "$a---$bn";
unset ($a, $b);
Var_dump ($a);
Var_dump ($b);
echo "##################### #n";
}

Foo ();
Foo ();
Foo ();
?>
To run the example, output:

1---1
Null
Null
#######################
1---2
Null
Null
#######################
1---3
Null
Null
#######################

unset () Global variables
As with unset () static variables, if you unset () a global variable in a function, only the local variable is destroyed, and the variable in the calling environment keeps the same value as before the call to Unset ().

Try to compare the following two examples:

The code is as follows Copy Code

function Destroy_foo () {
Global $foo;
Unset ($foo);
}

$foo = ' Bar ';
Destroy_foo ();
Echo $foo;
?>

function Destroy_foo () {
Global $foo;
unset ($GLOBALS [' foo ']);
}

$foo = ' Bar ';
Destroy_foo ();
Echo $foo;
?>


Running the first example will output: bar, and the second example will not have any output.

http://www.bkjia.com/PHPjc/628977.html www.bkjia.com true http://www.bkjia.com/PHPjc/628977.html techarticle The unset function is a function of PHP's own destruction variables, we introduced the use of unset to destroy static variables and global variables, but also out can destroy the array variable Oh, let's see ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.