search
HomeBackend DevelopmentPHP ProblemWhat does php unset mean?

What does php unset mean?

Jun 15, 2021 am 10:20 AM
unset

php unset is a function used to destroy a given variable. Its usage syntax is "void unset (mixed $var [, mixed $... ])", and its parameter "$var" means to destroy Variables.

What does php unset mean?

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

What does php unset mean?

The unset() function is used to destroy the given variable.

Syntax

void unset ( mixed $var [, mixed $... ] )

Parameter description:

$var: Variable to be destroyed.

Return value

No return value.

Example

<?php
// 销毁单个变量
unset ($foo);
 
// 销毁单个数组元素
unset ($bar[&#39;quux&#39;]);
 
// 销毁一个以上的变量
unset($foo1, $foo2, $foo3);
?>

If you unset() a global variable in a function, only the local variable will be destroyed, and the variables in the calling environment will maintain the same value before calling unset().

Example

<?php
function destroy_foo() {
    global $foo;
    unset($foo);
}
 
$foo = &#39;bar&#39;;
destroy_foo();
echo $foo;
?>

The output result is:

bar

If you want to unset() a global variable in the function, you can use the $GLOBALS array to achieve this:

Example

<?php
function foo() 
{
    unset($GLOBALS[&#39;bar&#39;]);
}
 
$bar = "something";
foo();
?>

If you unset() a variable passed by reference in a function, only the local variable will be destroyed, and the variables in the calling environment will maintain the same value before calling unset().

Example

<?php
function foo(&$bar) {
    unset($bar);
    $bar = "blah";
}
 
$bar = &#39;something&#39;;
echo "$bar\n";
 
foo($bar);
echo "$bar\n";
?>

The above routine will output:

something
something

If you unset() a static variable in the function, then the static variable will be destroyed inside the function. However, when this function is called again, this static variable will be restored to the value it had before it was last destroyed.

Example

<?php
function foo()
{
    static $bar;
    $bar++;
    echo "Before unset: $bar, ";
    unset($bar);
    $bar = 23;
    echo "after unset: $bar\n";
}
 
foo();
foo();
foo();
?>

The above routine will output:

Before unset: 1, after unset: 23
Before unset: 2, after unset: 23
Before unset: 3, after unset: 23

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What does php unset mean?. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment