Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Python, Lua and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Java, C, C++, Ruby, MySQL and Linux / Tomcat systems
Assigning variables by value and reference

Posted by Paula (Paula), 25 October 2005

I've just seen this example regarding the assigning of variables by value and by reference.

$variable1 = "Ryan";
$variable2 = $variable1;            // assign by value
print( "$variable1, $variable2<br>" );      // prints "Ryan, Ryan"
$variable2 = "Scott";
print( "$variable1, $variable2<br>" );      // prints "Ryan, Scott"
$variable3 = &$variable1;            // assign by reference
print( "$variable1, $variable3<br>" );      // prints "Ryan, Ryan"
$variable3 = "Katie";
print( "$variable1, $variable3<br>" );      //prints "Katie, Katie"

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?


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.


$first = "Hello";
$second = $first;
$first = "Greetings";  // Does NOT change $second

$person = "You";
$pname = &$person;
$person = "Paula";  // Changes both $person and $pname

print ("$first $person and welcome\n");
print ("$second $pname and welcome\n");

Gives me

Greetings Paula and welcome
Hello Paula and welcome

Posted by Paula (Paula), 25 October 2005
Ah! Now it makes sense!

How would such a thing be useful though?

Thanks a lot,

Posted by admin (Graham Ellis), 25 October 2005
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.


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

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2018: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01225 708225 • FAX: 01225 793803 • EMAIL: • WEB: • SKYPE: wellho