How to use the unset () function to destroy a single or multiple instance of a variable

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

<?php//destroys a single variable unset ($foo);//destroys a single array element unset ($bar [' Quux ']);//destroys 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 ().

<?phpfunction Destroy_foo () {global $foo; unset ($foo);} $foo = ' Bar ';d estroy_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 ().

<?phpfunction 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:

<?phpfunction 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---1nullnull###################### #1---2nullnull###################### #1---3nullnull#######################

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:

<?phpfunction Destroy_foo () {    global $foo;    Unset ($foo);} $foo = ' Bar ';d estroy_foo (); Echo $foo;? >
<?phpfunction Destroy_foo () {    global $foo;    unset ($GLOBALS [' foo ']);} $foo = ' Bar ';d estroy_foo (); Echo $foo;? >

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

Related Article

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.