Advertisement:

Author Topic: PHP Lesson 15 - variable referencing  (Read 31741 times)

Offline Parham

  • SMF Friend
  • Sophist Member
  • *
  • Posts: 1,317
  • Gender: Male
  • #!/usr/bin/perl
    • Online Node
PHP Lesson 15 - variable referencing
« on: September 29, 2003, 11:19:10 PM »
Occasionally you'll find yourself asking questions which would make your life easier.  One of these things is referencing.  As far as application uses go, there is no real need to ever reference a variable (or at least I don't think there is).  You reference if you're used to it and comfortable wit it, and don't if otherwise.  When working with variables, copies of their values are always used.

Code: [Select]
$variable1 = 'Something';
$variable2 = $variable1;

In the above code, we declare an initial variable, $variable1, and assign it a value.  Then we declare a new variable, $variable2, and we set it to $variable1.  What this does is it gets the value of $variable1 and assigns it to $variable2.  It does NOT assign $variable1 (the variable itself) to $variable2.

Code: [Select]
$variable1 = 'Something'; //initialize our variable
$variable2 = $variable1; //copy the value of $variable1 to $variable2 ("Something")
$variable1 = 'Something New'; //change the value of $variable1
echo "$variable1\n$variable2";
//prints
//"Something New"
//"Something"

In some cases, what you want to do is reference the variable, so when one changes, so does the other.  Referencing a variable means you don't copy its value, but instead you copy what it points to.  To reference something, you stick an ampersand (&) sign in front of the variable:

Code: [Select]
$variable1 = 'Something'; //initialize the variable
$variable2 = &$variable1; //this literally assigns the variable $variable1 to $variable2, so as $variable1 changes, so does $variable2

An example of this would be:

Code: [Select]
$variable1 = 'Something'; //initialize our variable
$variable2 = &$variable1; //reference to $variable1
$variable1 = 'Something New'; //change the value of $variable1; because $variable2 references to $variable1, $variable2 will ALSO change
echo "$variable1\n$variable2";
//prints
//"Something New"
//"Something New"

A good example of where referencing is used is with the "sort()" function.  In fact it's not used because sort() is an internal function, but this still gives you an example of referencing.  If you look at the sort() documentation, you'll notice that sort() works like this:

void sort ( array array [, int sort_flags])

It takes a variable in, but it doesn't return anything.  This means you can't do this with the sort() function:

Code: [Select]
$array = sort($array);

This is because in the sort() function, referencing is used.  The function uses a reference to the array.  The variable reference is sent in, and the function changes that same variable.

There ARE advantages to variable referencing...  If you reference variables, you save memory!!!  If you created a custom function which sorted an array, but not through referencing, this is what you would use:

Code: [Select]
$sorted = sort($unsorted);

Now imagine if your $unsorted array was EXTREMELY large.  This means that you'll have an equally extremely large array assigned to $sorted.  This means you'll be taking up twice as much space.  Of course you can always assign the return value to $unsorted, thus replacing the old one, but in cases such as the above with variable naming, things would get confusing.

I will end this lesson with two code examples.  One uses variable referencing, the other doesn't.  Both concatenate the word "Something" to a variable which already has a value of "Something".  Look at how they work differently:

Code: [Select]
//regular coding
$variable = 'Something';

function concatSomething($variable) {
  return $variable . 'Something';
}

echo concatSomething($variable);

Code: [Select]
//referencing
$variable = 'Something';

function concatSomething(&$variable) {
  $variable .= 'Something';
}

concatSomething($variable);
echo $variable;

Read more at http://www.php.net/manual/en/language.references.php
« Last Edit: April 17, 2004, 06:18:23 PM by Parham »

Offline writeto

  • Jr. Member
  • **
  • Posts: 341
  • Gender: Male
  • Professional Appreciator
Re:PHP Lesson 15 - variable referencing
« Reply #1 on: October 21, 2003, 05:49:58 PM »
Just a small note :

The only time I use references is to decrease overhead in function calls. This allows you to not have to temporarily create a copy of the variable you are passing. This is mostly used with large variables (such as an array). 

Andrew

Offline Bartrainer

  • Jr. Member
  • **
  • Posts: 146
  • Gender: Male
  • PHP: What do you want to do?
    • Online Bartending School
Re: PHP Lesson 15 - variable referencing
« Reply #2 on: April 17, 2004, 05:52:24 PM »
Code: [Select]
$variable1 = 'Something'; //initialize our variable
$variable2 = $variable1; //copy the value of $variable1 to $variable2 ("Something")
$variable1 = 'Something New'; //change the value of $variable1
echo "$variable1\n$variable2";
//prints
//"Something"
//"Something New"

I got:    Something New
             Something

Did i do something wrong, or is it a simple type. If it isn't a type it just adds to my NOOBIE confusion...lol

Everyone that says "it can't be done" is followed by someone that just did it.  The possibilities are as Powerful as your imagination...

"There are No bad cocktails, just bad drinkers."

Offline Parham

  • SMF Friend
  • Sophist Member
  • *
  • Posts: 1,317
  • Gender: Male
  • #!/usr/bin/perl
    • Online Node
Re: PHP Lesson 15 - variable referencing
« Reply #3 on: April 17, 2004, 06:18:00 PM »
this is actually a mistake in the tutorial.  thank you for correcting it.