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
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Assigning variables by value and reference

Posted by Paula (Paula), 25 October 2005
Hi,

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

<?php
$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?

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.

Code:
<?php

$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

Code:
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,
Paula

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.



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.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho