This article brings you a detailed introduction (code) about reference assignment and value assignment of PHP variables. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. .
// 定义一个变量 $a = range(0, 10000); var_dump(memory_get_usage()); // 定义变量b,将a变量的值赋值给b $b = $a; var_dump(memory_get_usage()); // 对a进行修改 // COW: Copy-On-Write $a = range(0, 10000); var_dump(memory_get_usage());
Output result:
int(989768) int(989856) int(1855608)
$a = range(0, 10000);
COW was first used in Unix 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.
When assigning a value to a variable through variable assignment, new memory will not be allocated to store the value of the new variable, but the memory will simply be shared through a counter. Only 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.
In many scenarios, PHP uses COW for memory optimization. For example: multiple assignments of variables, passing function parameters, and modifying actual parameters in the function body, etc.
// 定义一个变量 $a = range(0, 10000); var_dump(memory_get_usage()); // 定义变量b,将a变量的引用赋给b $b = &$a; var_dump(memory_get_usage()); // 对a进行修改 $a = range(0, 10000); var_dump(memory_get_usage());
int(989760) int(989848) int(989840)
2. Use
Used to display variable information. The xdebug extension needs to be installed.1. Pass-by-value assignment
$a = 1; xdebug_debug_zval('a'); // 定义变量b,把a的值赋值给b $b = $a; xdebug_debug_zval('a'); xdebug_debug_zval('b'); // a进行写操作 $a = 2; xdebug_debug_zval('a'); xdebug_debug_zval('b');
a: (refcount=1, is_ref=0)=1 a: (refcount=2, is_ref=0)=1 b: (refcount=2, is_ref=0)=1 a: (refcount=1, is_ref=0)=2 b: (refcount=1, is_ref=0)=1
$a = 1; xdebug_debug_zval('a');
a: (refcount=1, is_ref=0)=1
means that the number of references to the memory address pointed to by the variable becomes 1is_ref=0
means that the variable Instead of quoting
$a
to$b
,$b = $a;
$b = $a; xdebug_debug_zval('a'); xdebug_debug_zval('b');
a: (refcount=2, is_ref=0)=1 b: (refcount=2, is_ref=0)=1
indicates that the variable points to The number of references to the memory address becomes 2is_ref=0
, indicating that the variable is not a reference
## Write operation to variable
$a = 2; xdebug_debug_zval('a'); xdebug_debug_zval('b');
Copy after loginCopy after login
Output
a: (refcount=1, is_ref=0)=2 b: (refcount=1, is_ref=0)=1
When $a
performs a write operation, a new memory space will be allocated for variable$ato store the value of variable
$a.
At this time, the number of references to the memory addresses pointed to by$a
and
$bbecomes 1.
2. Reference assignment
$a = 1; xdebug_debug_zval('a'); // 定义变量b,把a的引用赋给b $b = &$a; xdebug_debug_zval('a'); xdebug_debug_zval('b'); // a进行写操作 $a = 2; xdebug_debug_zval('a'); xdebug_debug_zval('b');
a: (refcount=1, is_ref=0)=1 a: (refcount=2, is_ref=1)=1 b: (refcount=2, is_ref=1)=1 a: (refcount=2, is_ref=1)=2 b: (refcount=2, is_ref=1)=2
$a = 1; xdebug_debug_zval('a');
Copy after loginCopy after login
Output
a: (refcount=1, is_ref=0)=1
refcount=1
Indicates that the number of references to the memory address pointed to by the variable becomes 1is_ref=0
Indicates that the variable is not a reference
$b
,把$a
的引用赋给$b
,$b = &$a;
$b = &$a; xdebug_debug_zval('a'); xdebug_debug_zval('b');
输出
a: (refcount=2, is_ref=1)=1 b: (refcount=2, is_ref=1)=1
refcount=2
表示该变量指向的内存地址的引用个数变为2is_ref=1
表示该变量是引用
$a
进行写操作$a = 2;
$a = 2; xdebug_debug_zval('a'); xdebug_debug_zval('b');
输出
a: (refcount=2, is_ref=1)=2 b: (refcount=2, is_ref=1)=2
因为变量$a
和变量$b
指向相同的内存地址,其实引用。
对变量$a
进行写操作时,会直接修改指向的内存空间的值,因此变量$b
的值会跟着一起改变。
$a = 1; $b = &$a; // unset 只会取消引用,不会销毁内存空间 unset($b); echo $a;
输出
1
$a
,并将$a
的引用赋给变量$b
$a = 1; $b = &$a;
$b
unset($b);
$a
虽然销毁的$b
,但是$a
的引用和内存空间依旧存在。
echo $a;
输出
1
class Person { public $age = 1; } $p1 = new Person; xdebug_debug_zval('p1'); $p2 = $p1; xdebug_debug_zval('p1'); xdebug_debug_zval('p2'); $p2->age = 2; xdebug_debug_zval('p1'); xdebug_debug_zval('p2');
p1: (refcount=1, is_ref=0)=class Person { public $age = (refcount=2, is_ref=0)=1 } p1: (refcount=2, is_ref=0)=class Person { public $age = (refcount=2, is_ref=0)=1 } p2: (refcount=2, is_ref=0)=class Person { public $age = (refcount=2, is_ref=0)=1 } p1: (refcount=2, is_ref=0)=class Person { public $age = (refcount=1, is_ref=0)=2 } p2: (refcount=2, is_ref=0)=class Person { public $age = (refcount=1, is_ref=0)=2 }
$p1 = new Person;
$p1 = new Person; xdebug_debug_zval('p1');
输出
p1: (refcount=1, is_ref=0)=class Person { public $age = (refcount=2, is_ref=0)=1 }
refcount=1
表示该变量指向的内存地址的引用个数变为1is_ref=0
表示该变量不是引用
$p1
赋给$p2
$p2 = $p1; xdebug_debug_zval('p1'); xdebug_debug_zval('p2');
输出
p1: (refcount=2, is_ref=0)=class Person { public $age = (refcount=2, is_ref=0)=1 } p2: (refcount=2, is_ref=0)=class Person { public $age = (refcount=2, is_ref=0)=1 }
refcount=2
表示该变量指向的内存地址的引用个数变为2
$p2
中的属性age
进行写操作$p2->age = 2; xdebug_debug_zval('p1'); xdebug_debug_zval('p2');
输出
p1: (refcount=2, is_ref=0)=class Person { public $age = (refcount=1, is_ref=0)=2 } p2: (refcount=2, is_ref=0)=class Person { public $age = (refcount=1, is_ref=0)=2 }
因为php中对象本身就是引用赋值。对$p2
中的属性age
进行写操作时,会直接修改指向的内存空间的值,因此变量$p1
的age
属性的值会跟着一起改变。
/** * 写出如下程序的输出结果 * * $d = ['a', 'b', 'c']; * * foreach($d as $k => $v) * { * $v = &$d[$k]; * } * * 程序运行时,每一次循环结束后变量 $d 的值是什么?请解释。 * 程序执行完成后,变量 $d 的值是什么?请解释。 */
foreach
时$v
、$d[$k]
的值$k = 0 $v = 'a' $d[$k] = $d[0] = 'a'
此时,$v
和$d[0]
在内存中分别开辟了一块空间
![$v 和 $d[0] 在内存中分别开辟了一块空间](http://md.ws65535.top/xsj/201...
$v = &$d[0]
改变了 $v 指向的内存地址$v = &$d[0]
![$v = &$d[0] 改变了 $val 指向的内存地址](http://md.ws65535.top/xsj/201...
['a', 'b', 'c']
foreach
时$v
被赋值为 'b',此时$v
指向的内存地址与$d[0]
相同,且为引用,因此$d[0]
的值被修改为 'b'$v = 'b'
=>$d[0] = 'b'
![$v = ‘b’ => $d[0] = ‘b’](http://md.ws65535.top/xsj/201...
foreach
时$d[$k]
的值$k = 1 $d[$k] = $d[1] = 'b'
![$d[2] = ‘b’](http://md.ws65535.top/xsj/201...
$v = &$d[1]
改变了 $v 指向的内存地址$v = &$d[1]
![$v = &$d[1]](http://md.ws65535.top/xsj/201...
$d
的值['b', 'b', 'c']
foreach
时$v
被赋值为 'c',此时$v
指向的内存地址与$d[1]
相同,且为引用,因此$d[1]
的值被修改为 'c'$v = 'c'
=>$d[1] = 'c'
![$v = ‘c’ => $d[1] = ‘c’](http://md.ws65535.top/xsj/201...
foreach
时$d[$k]
的值$k = 2 $d[2] = 'c'
![$d[2] = ‘c’](http://md.ws65535.top/xsj/201...
$v = &$d[2]
改变了 $v 指向的内存地址$v = &$d[2]
![$v = &$d[2]](http://md.ws65535.top/xsj/201...
$d
的值['b', 'c', 'c']
$d = ['a', 'b', 'c']; foreach ($d as $k=>$v) { $v = &$d[$k]; print_r($d); } print_r($d);
Array ( [0] => a [1] => b [2] => c ) Array ( [0] => b [1] => b [2] => c ) Array ( [0] => b [1] => c [2] => c ) Array ( [0] => b [1] => c [2] => c )
相关推荐:
PHP变量赋值、代入给JavaScript中的变量,赋值javascript
The above is the detailed content of Detailed introduction to reference assignment and value assignment of PHP variables (code). For more information, please follow other related articles on the PHP Chinese website!