Phpunset () destroys one or more variables

Source: Internet
Author: User
The unset function is a built-in function for php to destroy variables. we introduced how to use unset to destroy static variables and global variables, and how to destroy array variables. let's look at the example below.

The unset function is a built-in function for php to destroy variables. we introduced how to use unset to destroy static variables and global variables, and how to destroy array variables. let's look at the example below.

Example 1. unset () example

The instance code is as follows:

  1. // Destroy a single variable
  2. Unset ($ foo );
  3. // Destroy a single array element
  4. Unset ($ bar ['quux ']);
  5. // Destroy more than one variable
  6. Unset ($ foo1, $ foo2, $ foo3 );
  7. ?>

The action of unset () in a function varies depending on the type of the variable to be destroyed.

If unset () is a global variable in the function, only the local variable is destroyed, and the variables in the call environment will keep the same value before the call of unset.

The instance code is as follows:

  1. Function destroy_foo (){
  2. Global $ foo;
  3. Unset ($ foo );
  4. }
  5. $ Foo = 'bar ';
  6. Destroy_foo ();
  7. Echo $ foo;
  8. ?>

The above example will output:

Bar

If unset () is a variable passed by reference in the function, only the local variable is destroyed, and the variables in the call environment will keep the same value before the call of unset.

The instance code is as follows:

  1. Function foo (& $ bar ){
  2. Unset ($ bar );
  3. $ Bar = "blah ";
  4. }
  5. $ Bar = 'something ';
  6. Echo "$ barn ";
  7. Foo ($ bar );
  8. Echo "$ barn ";
  9. ?>

The above example will output:

Something

Something

Unset () static variables. Strictly speaking, the use of unset () to destroy static variables only disconnects the reference between the variable name and the variable value.

Example:

  1. Function foo (){
  2. Static $ B;
  3. $ A ++;
  4. $ B ++;
  5. Echo "$ a --- $ bn ";
  6. Unset ($ a, $ B );
  7. Var_dump ($ );
  8. Var_dump ($ B );
  9. Echo "###################### n ";
  10. }
  11. Foo ();
  12. Foo ();
  13. Foo ();
  14. ?>

Run this example and output:

1---1

NULL

NULL

#######################

1---2

NULL

NULL

#######################

1---3

NULL

NULL

#######################

The unset () global variable is the same as the unset () static variable. If unset () is a global variable in the function, only the local variable is destroyed, the variables in the call environment will remain the same value before the call of unset.

Try to compare the following two examples:

The instance code is as follows:

  1. Function destroy_foo (){
  2. Global $ foo;
  3. Unset ($ foo );
  4. }
  5. $ Foo = 'bar ';
  6. Destroy_foo ();
  7. Echo $ foo;
  8. ?>
  1.  
  2. Function destroy_foo (){
  3. Global $ foo;
  4. Unset ($ GLOBALS ['foo']);
  5. }
  6. $ Foo = 'bar ';
  7. Destroy_foo ();
  8. Echo $ foo;
  9. ?>

The first example will output bar, while the second example will not have any output.

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.