for ($i=0; $i<$c;$i++)
{
$v1++;
echo $arrV[$i]."n";
}
both yield
1
2
and therefor cycle through the original objects (both $v1), which is, in terms of our aim, what we have been looking for.
(tested with php 4.1.3)
Here's a good little example of referencing. It was the best way for me to understand, hopefully it can help others.
$b = 2;
$a =& $b;
$c = $a;
echo $c;
// Then... $c = 2
I discovered something today using references in a foreach
<?php
$a1 = array('a'=>'a');
$a2 = array('a'=>'b');
foreach ($a1 as $k=>&$v)
$v = 'x';
echo $a1['a']; foreach ($a2 as $k=>$v)
{}
echo $a1['a']; ?>
After reading the manual this looks like it is meant to happen. But it confused me for a few days!
(The solution I used was to turn the second foreach into a reference too)
I ran into something when using an expanded version of the example of pbaltz at NO_SPAM dot cs dot NO_SPAM dot wisc dot edu below.
This could be somewhat confusing although it is perfectly clear if you have read the manual carfully. It makes the fact that references always point to the content of a variable perfectly clear (at least to me).
<?php
$a = 1;
$c = 2;
$b =& $a; $a =& $c; echo $a, " ", $b;
?>
In reply to lars at riisgaardribe dot dk,
When a variable is copied, a reference is used internally until the copy is modified. Therefore you shouldn't use references at all in your situation as it doesn't save any memory usage and increases the chance of logic bugs, as you discoved.
10/11 首页 上一页 8 9 10 11 下一页 尾页 |