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

引用的解释

时间:2011-05-16 15:55:22  来源:站内  作者:潘春会
'first', 'b'=>'second', 'c'=>'third');
foreach (
$arr as &$a); // do nothing. maybe?
foreach ($arr as $a);  // do nothing. maybe?
print_r($arr);
?>
Output:

Array
(
    [a] => first
    [b] => second
    [c] => second
)

Add 'unset($a)' between the foreachs to obtain the 'correct' output:

Array
(
    [a] => first
    [b] => second
    [c] => third
)

blake (15-Aug-2008 10:22)

 

 

 

To ffmandu13 at hotmail dot com, that's not correct. If you do a little research, you'll see that the Zend Engine employs "copy-on-write" logic, meaning that variables will be referenced instead of copied until it's actually written to. There's really no need to circumvent Zend's internal optimizations, since they're probably much more advanced than you think. Here's a good link to read over:

http://bytes.com/forum/thread769586.html

As stated, there's rarely (if ever) a need to use references for optimization purposes. When in doubt, remember that in most cases, the Zend Engine > you.

ffmandu13 at hotmail dot com (26-Jul-2008 10:06)

 

 

 

Just a side note to make developpers more careful about references.
References are pretty much useful when you try to optimize your code.

By the way, the time gained by referenced variables will depend of the variable type.

Integer and Float variables will work quite as fast referenced or not.
Whereas String and Array should be always passed by reference (you mostly divide by 10 the execution time).
Objects are automatically referenced by PHP.

Enjoy the references !

zzo38 (17-Jul-2008 04:08)

 

 

 

You can make references like pointers. Example:
<?php
  $a
=6;
 
$b=array(&$a); // $b is a pointer to $a
 
$c=array(&$b); // $c is a pointer to $b
 
$d=7;
 
$c[0][0]=9; // $a is 9
 
$c[0]=array(&$d); // $b is a pointer to $d
 
$c[0][0]=4
来顶一下
返回首页
返回首页
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
推荐资讯
如何找出DHCP地址池里未使用的IP地址
如何找出DHCP地址池里
国内常用的DNS列表
国内常用的DNS列表
Linux邮件服务器软件比较
Linux邮件服务器软件比
学用纯CSS打造可折叠树状菜单
学用纯CSS打造可折叠树
相关文章
栏目更新
栏目热门