Home > Backend Development > PHP Tutorial > What are the methods for referencing PHP variables? Summary of methods of php variable reference mechanism

What are the methods for referencing PHP variables? Summary of methods of php variable reference mechanism

不言
Release: 2023-04-03 20:08:01
Original
3318 people have browsed it

What this article brings to you is what are the methods of referencing PHP variables? This summary of the reference mechanism of PHP variables has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. Variable reference, "&" in php:

##

$a  = "hello world";//定义一个变量,下面赋值给$b
$b = $a;            //这一步实际上原理是会将变量$a复制拷贝一份,也就是内存中重新申请一个地址存储变量$b
Copy after login

Note: In PHP, using "

=" to directly assign a value actually copies the variable on the right to $b, which will generate a memory space. The result may be that the same content is stored in two copies in the memory.

2. Use the symbols “&” to quote

##

$a  = "hello world";
$b = &$a;
Copy after login
Note: When using references, the PHP engine will not copy a variable. In fact, it points the pointer to the address of $a in memory, and $b stores this pointer.

So when using references,
change the value of $b, $a will also change accordingly.

##

$a  = "hello world";
$b = &$a;
$b = "test new value";  //把b的值改掉,a的值也会跟着改变
echo $a;                //输出test new value,因为改变了b的值也会改变a的值。
Copy after login
I often see situations like this when defining functions:

function test (&$param){
    $param++;
}
$k = 8;
test($k);
echo $k; //结果$k的值被函数里面改变了,输出9。
Copy after login
Note: $param is preceded by a reference, so the parameters passed in will not be copied in memory. Instead, it directly references the original memory space. Therefore: If the variable value passed in using the symbol & is modified, the value in the original memory space will also be

changed.


#3. When destroying variables. It will not change the original value.

$a = 'd';
$b = &$a;
$b = 8;   
var_dump($b,$a);   //因为是引用了,所以把b的值改掉,a的值也跟着改为8了。
unset($b);         //调用unset删除b变量,a变量不会删除
var_dump($b,$a);   //输出null和8
Copy after login
Note: When calling unset to delete the $b variable, the php engine found from the variable symbol table: the variable I want to delete $b originally refers to the variable $a, which is difficult to delete, because once deleted, the $a variable is gone, so we first copy the $a variable and then delete the $b variable.

Related recommendations:

php uses open and fwrite to export files (code) in multiple formats

phpExcel in Solution to memory overflow when exporting files

The above is the detailed content of What are the methods for referencing PHP variables? Summary of methods of php variable reference mechanism. 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