note 75629 added to language.references.whatdo
| From: | amp at gmx dot info | Date: | Fri, 08 Jun 2007 17:59:57 +0000 |
| Subject: | note 75629 added to language.references.whatdo | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-127329@lists.php.net to get a copy of this message | ||
Something that might not be obvious on the first look:
If you want to cycle through an array with references, you must not use a simple value assigning
foreach control structure. You have to use an extended key-value assigning foreach or a for control
structure.
A simple value assigning foreach control structure produces a copy of an object or value. The
following code
$v1=0;
$arrV=array(&$v1,&$v1);
foreach ($arrV as $v)
{
$v1++;
echo $v."\n";
}
yields
0
1
which means $v in foreach is not a reference to $v1 but a copy of the object the actual element in
the array was referencing to.
The codes
$v1=0;
$arrV=array(&$v1,&$v1);
foreach ($arrV as $k=>$v)
{
$v1++;
echo $arrV[$k]."\n";
}
and
$v1=0;
$arrV=array(&$v1,&$v1);
$c=count($arrV);
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)
----
Server IP: 212.124.37.9
Probable Submitter: 194.37.75.240
----
Manual Page -- http://www.php.net/manual/en/language.references.whatdo.php
Edit -- https://master.php.net/note/edit/75629
Del: integrated -- https://master.php.net/note/delete/75629/integrated
Del: useless -- https://master.php.net/note/delete/75629/useless
Del: bad code -- https://master.php.net/note/delete/75629/bad+code
Del: spam -- https://master.php.net/note/delete/75629/spam
Del: non-english -- https://master.php.net/note/delete/75629/non-english
Del: in docs -- https://master.php.net/note/delete/75629/in+docs
Del: other reasons-- https://master.php.net/note/delete/75629
Reject -- https://master.php.net/note/reject/75629
Search -- https://master.php.net/manage/user-notes.php