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
=" 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;
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的值。
function test (&$param){
$param++;
}
$k = 8;
test($k);
echo $k; //结果$k的值被函数里面改变了,输出9。
$a = 'd'; $b = &$a; $b = 8; var_dump($b,$a); //因为是引用了,所以把b的值改掉,a的值也跟着改为8了。 unset($b); //调用unset删除b变量,a变量不会删除 var_dump($b,$a); //输出null和8
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!