Introduction to the problem
First let’s take a look at the assignment and reference in PHPProblem
<?php$a = 10;//将常量值赋给变量,会为a分配内存空间 $b = $a;//变量赋值给变量,是不是copy了一份副本,b也分配了内存空间呢? $c = &$a;//引用是不会为c分配空间的,c和a是共用一份空间的。?>What is your answer to the middle question? Before today, my answer was that memory space would be allocated for b. Because this is what I understand:
When assigning a value, it is regarded as an alias defined for a variable, and a reference to the memory space is added. Changing one of them will affect the other references. When using unset(), the reference to the variable memory space is only disconnected, and the memory space will not be released.
The = assignment is different. It will re-open a memory space to store a copy of the original variable. Modifications between the two will not affect each other.
<?php$a = 10; //将常量值赋给变量,会为a分配内存空间 $b = $a; //变量赋值给变量,是不是copy了一份副本,b也分配了内存空间呢? $c = &$a; //引用是不会为c分配空间的,c和a是共用一份空间的。 $a = 5;echo $c; //输出5,因为a和c 是指向同一个内存空间echo PHP_EOL; echo $b; //由于b是副本,对a的操作不会影响b,输出10?>Then if
$b = $a;//之后a 和 b 都不做任何改变,保持一致There is such a problem, if = After the assignment, neither variable has changed. If Isn't it a waste of memory if there are two copies?
This situation is actually avoided in PHP.
When assigning a variable to a new variable in PHP, memory space will not be allocated immediately for the new variable, but a reference to the memory space will be added. When any changes are made to the original variable or the new variable, a memory space will be allocated for the new variable.
<?php$a = 1;$b = $a; echo $a;//在此之前,b都是和a共用内存空间的。 $a = 2;//a作出了改变,此时b才会有自己的空间?>Each After installing xdebug, use xdebug_debug_zval(), you can see the zval structure:
As follows:
<?php $a = 1; $b = $a; echo $a;//在此之前,b都是和a共用内存空间的。xdebug_debug_zval('b'); $a = 2;//a作出了改变,此时b才会有自己的空间xdebug_debug_zval('b');?>Output:
b: (refcount=2, is_ref=0), int 1 b: (refcount=1, is_ref=0), int 1As you can see from the above results, before a is changed, the reference count is 2. After a is changed, b's reference count becomes 1. It's because b reallocates space.
The phenomenon described above is copy-on-write.
Copy-on-Write
Copy-on-Write(Copy-on-Write, also Abbreviated as COW), as the name suggests, it actually copies a copy of the memory for modification when writing. COW was first used in *nix systems to optimize thread and memory usage, and was later widely used in various programming languages, such as C++'s STL, etc. In the PHP kernel, COW is also the main memory optimization method. In the previous discussion about variables and memory, reference counting plays a crucial identification role in the destruction and recycling of variables. The purpose of reference counting is to enable COW to operate normally, thereby achieving optimal use of memory.
Advantages of copy-on-write: When assigning a value to a variable, it does not apply for new memory to store the value saved by the new variable. Instead, it simply shares the memory through a counter. Only when When the value of the variable pointed to by one of the references changes, new space is allocated to save the value content to reduce memory usage.
From the perspective of the underlying basic data structure of PHP
##ref_count and is_ref are defined in the zvalstructure; The is_ref identifier is whether the user uses & as a mandatory reference; Ref_count is a reference count, used to identify how many variables this zval is referenced, that is, an automatic reference copied when writing. When it is 0, it will be destroyed.
问题引入
首先来看看PHP中的赋值与引用问题
<?php$a = 10;//将常量值赋给变量,会为a分配内存空间 $b = $a;//变量赋值给变量,是不是copy了一份副本,b也分配了内存空间呢? $c = &$a;//引用是不会为c分配空间的,c和a是共用一份空间的。?>
对于中间的那个问题,你的答案是什么呢?在今天之前,我的答案是会为b分配内存空间。因为我是这么理解的:
&赋值的时候,视为一个变量定义了一个别名,增加了一个对内存空间的引用。改变其中一个,会影响其他的引用。而使用unset()时,只是断开了对变量内存空间的引用,内存空间不会释放。
而 = 赋值则不同,它会重新开辟一份内存空间存储原变量的副本。两者之间的修改不会相互影响。
而下面的程序则印证了这一点:
<?php $a = 10; //将常量值赋给变量,会为a分配内存空间 $b = $a; //变量赋值给变量,是不是copy了一份副本,b也分配了内存空间呢? $c = &$a; //引用是不会为c分配空间的,c和a是共用一份空间的。 $a = 5;echo $c; //输出5,因为a和c 是指向同一个内存空间echo PHP_EOL; echo $b; //由于b是副本,对a的操作不会影响b,输出10?>
那如果
$b = $a;//之后a 和 b 都不做任何改变,保持一致
有这么一个问题,如果 = 赋值之后,两个变量都不曾改变,如果是两份副本,岂不是太浪费内存?
PHP中实际上避免了这种情况。
PHP中将一个变量赋值给新变量时,不会立即为新变量分配内存空间,只是增加了对内存空间的引用。当原变量或者新变量作出任何改变时,才会为新变量 分配一块内存空间。
<?php$a = 1;$b = $a; echo $a;//在此之前,b都是和a共用内存空间的。 $a = 2;//a作出了改变,此时b才会有自己的空间?>
每个php变量存在一个叫”zval”的变量容器中。一个zval变量容器,除了包含变量的类型和值,还包括两个字节的额外信息。第一个是”is_ref”,是个bool值,用来标识这个变量是否是属于引用集合(referenceset)。通过这个字节,php引擎才能把普通变量和引用变量区分开来,由于php允许用户通过使用&来使用自定义引用,zval变量容器中还有一个内部引用计数机制,来优化内存使用。第二个额外字节是”refcount”,用以表示指向这个zval变量容器的变量(也称符号即symbol)个数。当”refcount”的值是1时,”is_ref”的值总是FALSE.
安装xdebug之后,利用xdebug_debug_zval(),可以看到zval结构:
如下:
<?php $a = 1; $b = $a; echo $a;//在此之前,b都是和a共用内存空间的。xdebug_debug_zval('b'); $a = 2;//a作出了改变,此时b才会有自己的空间xdebug_debug_zval('b');?>
输出:
b: (refcount=2, is_ref=0), int 1 b: (refcount=1, is_ref=0), int 1
由上面的结果可以看到,在a作出改变之前,引用计数是2 ,当a作出改变之后,b的引用计数变为1,是因为b重新分配了空间。
上面说描述的现象就是写时复制。
写时复制
写时复制(Copy-on-Write,也缩写为COW),顾名思义,就是在写入时才真正复制一份内存进行修改。 COW最早应用在*nix系统中对线程与内存使用的优化,后面广泛的被使用在各种编程语言中,如C++的STL等。 在PHP内核中,COW也是主要的内存优化手段。 在前面关于变量和内存的讨论中,引用计数对变量的销毁与回收中起着至关重要的标识作用。 引用计数存在的意义,就是为了使得COW可以正常运作,从而实现对内存的优化使用。
写时复制优点:是通过赋值的方式赋值给变量时不会申请新内存来存放新变量所保存的值,而是简单的通过一个计数器来共用内存,只有在其中的一个引用指向变量的值发生变化时才申请新空间来保存值内容以减少对内存的占用。
从PHP底层基础数据结构来看
ref_count和is_ref是定义于zval结构体中;
is_ref标识是不是用户使用 & 的强制引用;
ref_count是引用计数,用于标识此zval被多少个变量引用,即写时复制的自动引用,为0时会被销毁。
The above is the detailed content of Code example sharing of Copy On Write in PHP. For more information, please follow other related articles on the PHP Chinese website!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version
SublimeText3 Linux latest version

Notepad++7.3.1
Easy-to-use and free code editor

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6
Visual web development tools
