'first', 'b'=>'second', 'c'=>'third');
foreach ($arr as &$a); foreach ($arr as $a); 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
)
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.
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 !
You can make references like pointers. Example:
<?php
$a=6;
$b=array(&$a); $c=array(&$b); $d=7;
$c[0][0]=9; $c[0]=array(&$d); $c[0][0]=4
8/24 首页 上一页 6 7 8 9 10 11 下一页 尾页 |