Home Backend Development PHP Tutorial php unset() destroys single or multiple variables_PHP tutorial

php unset() destroys single or multiple variables_PHP tutorial

Jul 13, 2016 pm 05:14 PM
php unset function variable Multiple us yes of Bring your own destroy

The unset function is a function that comes with PHP to destroy variables. We introduced the method of using unset to destroy static variables and global variables. At the same time, we also introduced the method of destroying array variables. Let's see an example below.

Example 1. unset() example

The code is as follows Copy code
 代码如下 复制代码

<?php
// 销毁单个变量
unset ($foo);

// 销毁单个数组元素
unset ($bar['quux']);

// 销毁一个以上的变量
unset ($foo1, $foo2, $foo3);
?>

<?php

// Destroy a single variable

unset ($foo);

//Destroy a single array element
代码如下 复制代码

<?php
function destroy_foo() {
global $foo;
unset($foo);
}

$foo = ‘bar’;
destroy_foo();
echo $foo;
?>

unset ($bar['quux']);

// Destroy more than one variable

unset ($foo1, $foo2, $foo3);

?>

The behavior of unset() in a function will vary depending on the type of variable you want to destroy.
 代码如下 复制代码

<?php
function foo(&$bar) {
unset($bar);
$bar = “blah”;
}

$bar = ‘something’;
echo “$barn”;

foo($bar);
echo “$barn”;
?>
上边的例子将输出:

something
something

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().

The code is as follows Copy code
<?php

function destroy_foo() {

global $foo;
代码如下 复制代码

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

foo();
foo();
foo();
?>
运行该例子,输出:

1---1
NULL
NULL
#######################
1---2
NULL
NULL
#######################
1---3
NULL
NULL
#######################

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 will be destroyed, and the variables in the calling environment will retain the same value before calling unset().
The code is as follows Copy code
<?php<🎜> 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, using unset() to destroy a static variable only breaks the reference between the variable name and the variable value. Example:
The code is as follows Copy code
<?php<🎜> function foo() {<🎜> ​ static $b;<🎜> $a++;<🎜> $b++;<🎜> echo "$a---$bn";<🎜> ​ unset($a,$b);<🎜> var_dump($a);<🎜> var_dump($b);<🎜> echo "######################n";<🎜> }<🎜> <🎜>foo();<🎜> foo();<🎜> foo();<🎜> ?> Running this example, output: 1---1 NULL NULL ####################### 1---2 NULL NULL ####################### 1---3 NULL NULL #######################

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

Try to compare the following two examples:

function destroy_foo() { global $foo;
The code is as follows
 代码如下 复制代码

<?php
function destroy_foo() {
global $foo;
unset($foo);
}

$foo = 'bar';
destroy_foo();
echo $foo;
?>

<?php
function destroy_foo() {
global $foo;
unset($GLOBALS['foo']);
}

$foo = 'bar';
destroy_foo();
echo $foo;
?>

Copy code


<?php
​ unset($foo);

}

$foo = 'bar'; echo $foo; ?> <?php function destroy_foo() { global $foo; ​ unset($GLOBALS['foo']); } $foo = 'bar'; destroy_foo();
echo $foo;

?>
Running the first example will output: bar , while the second example will not output anything. http://www.bkjia.com/PHPjc/628977.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/628977.htmlTechArticleThe unset function is a function that comes with PHP to destroy variables. We introduced the use of unset to destroy static variables and globals. The variable method can also destroy array variables. Let’s see below...
Statement of this Website
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 Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

See all articles