help for a newb

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kc64
Forum Newbie
Posts: 1
Joined: Sat May 16, 2009 5:47 pm

help for a newb

Post 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.
Last edited by Benjamin on Mon May 18, 2009 9:54 am, edited 2 times in total.
Reason: Changed code type from text to php.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: help for a newb

Post 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
Post Reply