Detailed explanation of PHP reference variable knowledge

不言
Release: 2023-03-25 06:42:01
Original
2478 people have browsed it


This article mainly introduces the detailed knowledge about PHP reference variables. It has a certain reference value. Now I share it with everyone. Friends in need can refer to it

  • Concept: Reference in PHP means accessing the same variable content with different names;

  • Definition method: PHP uses '& 'Define reference variables;

  • #When references are not used, variables adopt a copy-on-write mechanism (COW): a copy of the memory will be copied for modification when writing, for example

//定义一个变量
$a = range(0,1000);
var_dump(memory_get_usage()); //打印内存使用量
//定义一个变量b
$b = $a; //此时$b和$a 指向同一个内存空间
var_dump(memory_get_usage());
//当a或b发生写入时,才会复制一块内存进行修改
$a = range(0,1000);
var_dump(memory_get_usage());
Copy after login

Running results: The memory usage was similar for the first and second printing times, but there was a significant change for the third time, indicating that when a performed a write operation, the memory usage occurred copy.


  • #When using a reference, the variable will point to the same memory space and will not change during the write operation.

//定义一个变量
$a = range(0,1000);
var_dump(memory_get_usage()); //打印内存使用量
//定义一个变量b
$b = &$a; //将a的空间赋值给b,a与b指向同一空间
var_dump(memory_get_usage());
//当a或b发生写入时,内存不会发生复制
$a = range(0,1000);
var_dump(memory_get_usage());
Copy after login

Running results: The memory has not changed significantly

##Verified through the zval variable container

//通过zval变量容器打印
$a = range(0,3);
xdebug_debug_zval('a');//打印 指向内存空间的变量数,和是否被引用
$c =&$a;
xdebug_debug_zval('a');
$c = range(0,3);
xdebug_debug_zval('a');
Copy after login

Running results:

When writing to c, there are still 2 variables pointing to the memory, and copy-on-write does not occur



    ##unset will only cancel the reference and will not destroy the space
  • In PHP, the object itself is passed by reference and does not need to use reference symbols.
  • class Person{
        public $name  = "zhangsan";
    }
    $p1 = new Person();
    xdebug_debug_zval('p1');
    
    $p2 = $p1;
    xdebug_debug_zval('p1');
    
    $p2->name = "lesi";
    xdebug_debug_zval('p1');
    Copy after login


    Related recommendations:

    Detailed explanation of php reference definition and reference passing parameter example usage

    The above is the detailed content of Detailed explanation of PHP reference variable knowledge. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!