Table of Contents
回复内容:
Home Backend Development PHP Tutorial PHP unset函数好奇怪

PHP unset函数好奇怪

Jun 06, 2016 pm 08:43 PM
php

unset传递的值是一个对象或者值的复制(比如传递数组,传递的是一个新的复制,而不是引用),结果原对象却被销毁了。好奇怪。
大神快来解答这个问题。以前一直用unset,今天突然想到了这个问题。
比如:

<br><?php


$a='hello';
$b=true;
unset($b);
unset($a);
echo $b;
echo $a;

?>
Copy after login
Copy after login

这时就会报出Undefined variable的错误
正如舞林所说,这种销毁变量的方式有可能是将引用计数减一。
但是这样做:

<?php
$a='hello';
$b=&$a;
unset($a);
echo $a;

?>
Copy after login
Copy after login

这个时候,$a依然被销毁。报出Undefined variable的错误的错误。
如果unsetb,来看看结果:

<?php


$a='hello';
$b=&$a;
unset($b);
echo $a;

?>
Copy after login
Copy after login

只是销毁了$b,$b虽然是$a的引用,但是$a没有被销毁。打印出hello
所以unset的机制并没有那么简单。。

回复内容:

unset传递的值是一个对象或者值的复制(比如传递数组,传递的是一个新的复制,而不是引用),结果原对象却被销毁了。好奇怪。
大神快来解答这个问题。以前一直用unset,今天突然想到了这个问题。
比如:

<br><?php


$a='hello';
$b=true;
unset($b);
unset($a);
echo $b;
echo $a;

?>
Copy after login
Copy after login

这时就会报出Undefined variable的错误
正如舞林所说,这种销毁变量的方式有可能是将引用计数减一。
但是这样做:

<?php
$a='hello';
$b=&$a;
unset($a);
echo $a;

?>
Copy after login
Copy after login

这个时候,$a依然被销毁。报出Undefined variable的错误的错误。
如果unsetb,来看看结果:

<?php


$a='hello';
$b=&$a;
unset($b);
echo $a;

?>
Copy after login
Copy after login

只是销毁了$b,$b虽然是$a的引用,但是$a没有被销毁。打印出hello
所以unset的机制并没有那么简单。。

PHP unset函数好奇怪

第一段代码 a和b是两块不同的内存 所以unset掉两者 根本没影响
第二段代码 b引用a 使得对应的zval ref_count+1 is_ref +1 此时不管unset掉a或者b 只是断了一根引用对另外的那个值没影响 还是指向的那块区域

变量引用次数-1,只要有对该内存块的引用,该内存块就不会被销毁

update:
昨天写答案提交发现服务器在调皮的维护,以为没发出去呢...
TIPI有写具体的内容:
http://www.php-internals.com/book/?p=chapt03/03-01-00-variables-structure
//变量结构体

http://www.php-internals.com/book/?p=chapt03/03-06-01-var-define-and-init
//变量的赋值和销毁,这里详细讲解了引用计数

我觉得你说得好像不成立。。

$o = new stdClass();
$o->var = 123;
$new_o = $o; //等于$new_o = & $o;
unset($new_o);
var_dump($o);
Copy after login

对象赋值本身就是引用赋值,但是unset 引用的变量 只是把引用给销毁了,并不会销毁原变量

我的理解是这个样子

...你 确定?

------------update-------------
把评论里的拿上来
unset只是断开了变量 名和值 之间的绑定

引用:
"该函数只有在变量值所占空间超过256字节长的时候才会释放内存"
&&
"有当指向该值的所有变量(比如有引用变量指向该值)都被销毁后,地址才会被释放"

unset($a)无论你$a是怎么得到的(直接赋值$a=true、传值$a=$b、传址$a=&$b),以及对$a进行过什么操作(传值给别的变量$b=$a或传址$b=&$a),它都会断掉$a的引用,并把$a抹成null。
至于被赋值的那个对象是不受影响的,引用计数自己会处理好。

同意楼上几位的回答,如果LZ的问题依然存在,不妨贴出你的代码可以更好的说明情况。

不过我想通过以下代码给LZ提个醒:

$foo = true;
$bar = &$foo;
unset($foo);
var_dump($bar);  // 结果是true,而不是null
Copy after login

另外反过来一样

$foo = true;
$bar = &$foo;
unset($bar);
var_dump($foo);  // 结果依然是true,而不是null
Copy after login

可以看出:即时向unset传递一个变量的引用,也不会把该变量销毁
所以我不太能理解LZ的问题是怎么一回事……

这段代码是我从别处搜索来的,我认为应该能够解决你的疑问!
如果在函数中 unset() 一个通过引用传递的变量,则只是局部变量被销毁,而在调用环境中的变量将保持调用 unset() 之前一样的值。

<code>  <?php
    function foo(&$bar) {
     unset($bar);
     $bar = "blah";
    }

$bar = 'something';
echo "$bar ";

foo($bar);
echo "$bar ";
?> 
</code>
Copy after login

上边的例子将输出:

something
something

楼主第二个测试,可以输出$b 试试,一样没有被销毁

参考这个
http://php.net/manual/zh/features.gc.refcounting-basics.php

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 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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

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 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

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

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

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

To work on file upload we are going to use the form helper. Here, is an example for file upload.

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

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

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

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

See all articles