FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Doubt regarding an array of references...
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Doubt regarding an array of references... [message #177493] Mon, 02 April 2012 16:17 Go to previous message
Leonardo Azpurua is currently offline  Leonardo Azpurua
Messages: 46
Registered: December 2010
Karma:
Member
Hi,

I am coding a simple parser, where a construct is made of a fixed number
sequence of varIDs or quoted literals that may or may not be separated by
punctuators.

In order to avoid a repetition of similar statements, I decided to try if I
could store the references of the variables where each value should be
stored and then perform a loop in order to extract and store the symbols.

The try was this little program:

$a = $b = $c = "nil";
echo "a = $a, b = $b, c = $c<br>\n";

$d = array(&$a, &$b, &$c);
for ($n = 0; $n < count($d); $n++) $d[$n] = $n;
echo "a = $a, b = $b, c = $c<br>\n";

for ($n = 0; $n < count($d); $n++) $d[$n] = ($n + 100);
echo "a = $a, b = $b, c = $c<br>\n";

$n = 200;
foreach ($d as &$nx) $nx = $n++;; // why &$nx ?
echo "a = $a, b = $b, c = $c<br>\n";

The output is this:

a = nil, b = nil, c = nil
a = 0, b = 1, c = 2
a = 100, b = 101, c = 102
a = 200, b = 201, c = 202

so it works as needed.

My question is regarding the instruction with the comment.

My first try was to write foreach ($d as $nx), since each element in the $d
array is supposed to be already holding a reference.

I find this confusing. Wouldn't &$nx holde the reference to the array
element, instead of the reference contained within the array element?

Thanks in advance.

--
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: A little tip
Next Topic: How to etablish an SSH2 tunnel with php ?
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Fri Nov 22 22:41:00 GMT 2024

Total time taken to generate the page: 0.03657 seconds