Helping ordinary people create extraordinary websites!
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Email:

Using Variable Referencing

By Darren W. Hedlund
2005-03-30


Using Variable Referencing

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 with it, and don't if otherwise. When working with variables, copies of their values are always used.

$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.

$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:

$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:

$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:

$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:

$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:

//regular coding

$variable = 'Something';

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

echo concatSomething($variable);


//referencing
$variable = 'Something';

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

concatSomething($variable);
echo $variable;


Tutorial Pages:
» Using Variable Referencing


 | Bookmark
Related Tutorials:
» Zend Framework Tutorial
» Port Scanning and Service Status Checking in PHP
» Web Database Access from Desktop Applications
» CubeCart 3.0 Installation and Configuration
» PHP Site Search Made Easy
» Installing and Configuring Drupal 6.1

Ask A Question
characters left.