reference引用

/***************by Jiangong SUN***************/

 

<?php
//$a =& $b;
//$a和$b完全相等,指向了同一个内存地址。如果a改变了值,b也将改变值。
//Note: $a and $b are completely equal here. $a is not pointing to $b or vice versa. $a and $b are pointing to the same place.
//Note: If arrays with references are copied, their values are not dereferenced. This is valid also for arrays passed by value to functions.
//Note: If you assign, pass, or return an undefined variable by reference, it will get created.

//toto tata指向同一个内存地址,改变toto或tata的值,另一个变量的值也随之改变
$toto = 'hello';
$tata =& $toto;
$tata = 'new world';
echo $tata; // new world
echo "<br>";
echo $toto;  // new world
echo "<br>";

//One important thing to note is that only named variables may be assigned by reference.
$foo = 25;
$bar = &$foo;      // This is a valid assignment.
//$bar =&(24 * 7);  // Invalid; references an unnamed expression.
function test()
{
   return 25;
}
//$bar = &test();    // Invalid.

function test1(&$a)
{
$a=$a+100;
}
$b=1;
echo $b;//输出1
test1($b);  //这里$b传递给函数的其实是$b的变量内容所处的内存地址,通过在函数里改变$a的值 就可以改变$b的值了
echo "<br>";
echo $b;//输出101
echo "<br>";

 

//unset When you unset the reference, you just break the binding between variable name and variable content.
//This does not mean that variable content will be destroyed.
$a = 1;
$b =& $a;
unset($a);
echo "<br />";
echo $a; //输出 undefined variable
echo $b;  //输出1

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值