Page 1 of 1

help for a newb

Posted: Sat May 16, 2009 5:54 pm
by kc64
Hello, all. I'm seeking help while trying to learn the language.

I bought http://www.amazon.com/Learning-MySQL-St ... se-Driven/ and http://www.amazon.com/Programming-PHP-R ... 596006810/ recently from Borders.

On page 105 of the first book, I see this code:

Code: Select all

 
$some_variable = "Hello world!";
$some_reference = &$some_variable;
$some_reference = "Guten Tag world!";
echo $some_variable;
echo $some_reference;
which supposedly has the output:

Code: Select all

 
Guten Tag world!Guten Tag world!
What the heck?! Why should the second assignment of $some_reference have a different effect than the first?

Coming from a Python/Perl/C++ perspective, I find it odd that there is no reference de-referencer, if I can call it that.

Please help me understand how to better look at this.

Thanks.

Re: help for a newb

Posted: Sat May 16, 2009 6:33 pm
by Benjamin
The & sign effectively assigns a pointer to the original variable. Essentially both variables are the same but with different names. Changing one changes the other. This is the point.

See: http://us3.php.net/manual/en/language.r ... s.pass.php