I don't understand how this works. That the ampersand makes the variable take its original value makes sense - it's that the variable it's resetting - variable1 - was never changed.

Should the line reading "$variable3 = &$variable1;" not be "$variable3 = &$variable2;" for this example to work?

Thanks,Paula

Posted by admin (Graham Ellis), 25 October 2005

Hi, Paula ... welcome to Opentalk.

A good question on an oft-confusing subject.

If you assign a variable in the "normal" way, you're copying the contents of the variable so that you end up with another variable and another copy of the value. Change one of the two variables created in this way, and you've only changed that one.

If you use the extra & character, you're assigning a second name to the same variable .... so that if you change the contents via either name, both variables will apparently be changed; really, they're the same variable though ... with two names.

It's useful because you can write a whole lot of complicated code to deal with a variable such as "$current", then point $current at each of the elements of an array you want to process in turn ... welcome to the beginnings of the OO world

Posted by Paula (Paula), 26 October 2005

Mmmm, that sounds dead useful. Thanks a lot for that.

Paula

This page is a thread posted to the opentalk forum
at www.opentalk.org.uk and
archived here for reference. To jump to the archive index please
follow this link.