Unset ()

Source: Internet
Author: User
Void Unset (Mixed $ VaR [, Mixed $ VaR [, $... ])

Unset ()Destroys the specified variable. Note that in PHP 3,Unset ()ReturnsTrue(Actually an integer value of 1), while in PHP 4,Unset ()It is no longer a real function: it is now a statement. In this way, no return value is returned and an attempt is made to obtainUnset ()Will cause parsing errors.

Example #1Unset ()Example

// destroy a single variable
unset ( $ Foo );

// destroy a single array element
unset ( $ bar [ 'quux ' ]);

// Destroy more than one variable
Unset ($ Foo1,$ Foo2,$ Foo3);
?>

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

IfUnset ()A global variable is destroyed, while the variables in the call environment will keep callingUnset ()The same value.

<? PHP
FunctionDestroy_foo(){
Global$ Foo;
Unset ($ Foo);
}

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

The above example will output:

Bar

IfUnset ()A variable passed through the reference is only the local variable is destroyed, and the variable in the call environment will remain calledUnset ()The same value.

<? PHP
Function Foo (& $ Bar ){
Unset ( $ Bar );
$ Bar = "Blah" ;
}

$ Bar='Something';
Echo"$ Bar\ N";

Foo($ Bar);
Echo"$ Bar\ N";
?>

The above example will output:

 
Something something

If unset () is a static variable in the function, the static variable is destroyed within the function. However, when this function is called again, the static variable is restored to the value before the previous destruction.

function Foo () {
static $ A ;< br> $ A +++;
echo " $ A \ n" ;< br> unset ( a A );
}< P>

Foo();
Foo();
Foo();
?>

The above example will output:

1 2 3

If you wantUnset ()A global variable that can be used$ GlobalsArray:

function Foo () {
unset ( $ globals [ 'bar' ]);
}< P>

$ Bar="Something";
Foo();
?>

Note: It is a language constructor rather than a function and cannot be called by variable functions.

 

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.