免费邮箱 |加入收藏 | 会员中心 | 我要投稿 | RSS
您当前的位置:首页 > PHP专区 > PHP技巧

引用不是什么

时间:2011-05-16 15:57:52  来源:站内  作者:潘春会


<?php
$b
=1; $d=2;
$a =& $b;
$c =& $d;
// now a=b=1; c=d=2;
$b =& $c;
// now a=1, b=c=d=2;
?>

It's also important to treat =& as its own operator, though you can stick spaces in the middle. The & does not bind to the variable, as shown by
<?php
$a
= (&$b); // parse error
?>

jagatpreet (31-Jan-2006 03:27)

 

 

 

In response to the example by mdiricks.

Extending the example given by mdiricks, the following code provides an explains the concept of re-referencing that is involved in making a call to function foo with the prototype foo(& var):

<!-- C re-referenced -->
<?

$a = 'eh';
$b = & $a;// $b == 'eh'
$c = & $b;// $c == 'eh'
$d = 'meh';

echo "$a = $an";
echo "$b = $bn";
echo "$c = $cn";
echo "$d = $dn";

$c = & $d ;// $c == 'meh'
echo "n";

echo "$a = $an";
echo "$b = $bn";
echo "$c = $cn";
echo "$d = $dn";

?>

<!-- Value of c changed -->
<?

$a = 'eh';
$b = & $a;// $b == 'eh'
$c = & $b;// $c == 'eh'
$d = 'meh';

echo "$a = $an";
echo "$b = $bn";
echo "$c = $cn";
echo "$d = $dn";

$c = 'meh' ;// $c == 'meh'. And also, $a = $b == 'meh'
echo "n";

echo "$a = $an";
echo "$b = $bn";
echo "$c = $cn";
echo "$d = $dn";

?>

This results in the following o/p:
<!-- C re-referenced -->
$a = eh
$b = eh
$c = eh
$d = meh

$a = eh
$b = eh
$c = meh
$d = meh

<!-- Value of c changed -->
$a = eh
$b = eh
$c = eh
$d = meh

$a = meh
$b = meh
$c = meh
$d = meh

DanielSan88 (28-Oct-2005 11:10)

 

 

 

I can see why there is a big debate on this. I'm hoping this will clarify it a little.

In PHP, as far as I can understand, when you assign a variable in the first place,
that variable is a refrence to the value that the variable contains.

<?php

$var
= "String"; // Creates the string "String" somewhere in memory.
echo $var;

/* When the echo function is called, it pulls the value of $var out of memory from wherever PHP decided to put it.
PHP then ships this value off to your standard output. */

?>

Anyway, when you create a reference to the variable $var,
it doesn't reference the variable's name, but rather its contents.

For example:

<?php

$var2
= & $var;
echo
$var2;
来顶一下
返回首页
返回首页
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
推荐资讯
如何找出DHCP地址池里未使用的IP地址
如何找出DHCP地址池里
国内常用的DNS列表
国内常用的DNS列表
Linux邮件服务器软件比较
Linux邮件服务器软件比
学用纯CSS打造可折叠树状菜单
学用纯CSS打造可折叠树
相关文章
栏目更新
栏目热门