jump to navigation

Working with References in PHP October 9, 2010

Posted by Tournas Dimitrios in PHP.

You  already known that you can pass information to a function in the form of arguments, as well as return information from a function to its calling code using the   return  statement. When you do either of these things, PHP actually passes copies of the information to and from the function; this is known as passing and returning   by value .
Most of the time this isn ’ t a problem, but sometimes you want your function to work on the original information, rather than on a copy. Consider the following example:

function resetCounter( $c ) {
$c = 0;
$counter = 0;
echo “$counter < br/ > ”;  // Displays “3”
resetCounter( $counter );
echo “$counter < br/ > ”;  // Displays “3”


This code defines a function,   resetCounter() , that resets its argument to zero. A   $counter  variable is then initialized to zero and incremented three times. As you ’ d expect, the value of   $counter  at this point is   3 .   resetCounter()  is then called, passing in   $counter , the variable to reset. However, as the second   echo  statement shows,   $counter  has not been reset by the function. This is because the parameter   $c  inside   resetCounter()  merely holds a copy of the information stored in   $counter . So when the function sets   $c  to zero, it doesn ’ t affect the value of   $counter  at all.
Fortunately, PHP provides a mechanism known as references that you can use to work around such issues.
A reference is a bit like a shortcut or alias to a file on your hard drive. When you create a reference to a PHP variable, you now have two ways to read or change the variable ’ s contents — you can use the variable name, or you can use the reference. Here ’ s a simple example that creates a reference to a variable:

$myVar = 100;
$myRef = & $myVar;
echo $myRef . “ < br/ > ”; // Displays “101”
echo $myVar . “ < br/ > ”; // Displays “101”


Now that you know what references are, and how to create them, it ’ s time to look at how you can pass references into and out of your functions.

Passing References to  Functions :

By passing a reference to a variable as an argument to a function, rather than the variable itself, you pass the argument   by reference , rather than by value. This means that the function can now alter the original value, rather than working on a copy.
To get a function to accept an argument as a reference rather than a value, put an ampersand (   &   ) before the parameter name within the function definition .

Now that you know this, you can fix the earlier counter example by using a reference:

function resetCounter( & $c ) {
  $c = 0;
$counter = 0;
echo “$counter < br/ > ”;  // Displays “3”
resetCounter( $counter );
echo “$counter < br/ > ”;  // Displays “0”

The only change in the script is in the first line:
function resetCounter(  & $c ) {
Adding the ampersand before the   $c  causes the   $c  parameter to be a reference to the passed argument (  $counter  in this example). Now, when the function sets   $c  to zero, it ’ s actually setting the value of $counter  to zero, as can be seen by the second   echo  statement.
Many built  – in PHP functions accept references in this way. For example, PHP  ’ s   sort()  function, changes the array you pass to it, sorting its elements in order. The array is passed
in by reference rather than by value, so that the function can change the array itself, rather than a copy of it.

Returning References from  Functions :

As well as passing variables by reference into functions, you can also get functions to return references, rather than values. To do this, you place an ampersand before the function name in your function definition. Then, when you return a variable with the   return  statement, you pass a reference to that variable back to the calling code, rather than the variable  ’ s value:

function & myFunc(){
  // (do stuff)
  return $var;  // Returns a reference to $var
// Here ’ s an example that shows return - by - reference in action:
$myNumber = 5;
function & getMyNumber() {
  global $myNumber;
  return $myNumber;
$numberRef = & getMyNumber();
echo “\$myNumber = $myNumber < br/ > ”; // Displays “6”
echo “\$numberRef = $numberRef < br/ > ”; // Displays “6”

Return – by – reference is used quite often in languages such as C++, because it ’ s the easiest way to return complex data structures such as arrays and objects. However, because PHP lets you return pretty much anything with its   return  statement, and automatically returns objects by reference anyway , you probably won  ’ t use return – by – reference that much in PHP.

What about references in actionscript functions ? :

To be passed by value means that the value of the argument is copied into a local variable for use within the function.
To be passed by reference means that only a reference to the argument is passed instead of the actual value. No copy of the actual argument is made. Instead, a reference to the variable passed as an argument is created and assigned to a local variable for use within the function. As a reference to a variable outside the function, the local variable gives you the ability to change the value of the original variable.
In ActionScript 3.0, all arguments are passed by reference, because all values are stored as objects. However, objects that belong to the primitive data types, which includes Boolean, Number, int, uint, and String, have special operators that make them behave as if they were passed by value. For example, the following code creates a function named passPrimitives() that defines two parameters named xParam and yParam, both of type int. These parameters are similar to local variables declared inside the body of the passPrimitives() function. When the function is called with the arguments xValue and yValue, the parameters xParam and yParam are initialized with references to the int objects represented by xValue and yValue. Because the arguments are primitives, they behave as if passed by value. Although xParam and yParam initially contain only references to the xValue and yValue objects, any changes to the variables within the function body generate new copies of the values in memory.Well  this theory sounds very confusing , better see the code that follows  :

var num:int = 3 ;
var num2:Array = [4];

trace(num);	   //returns 3
trace(num2[0]); //returns 4
function increasNumber(val1:int , val2:Array):void{
var num:int = val1 ;
var arr:Array = val2;
	num ++;
	arr[0] = 7;

increasNumber(num , num2);
trace("the value after the function call is :" + num ) ;   //returns 3
trace("the value of the array after the func is :" + num2[0]); //returns 7

Read more on PHP.net  

Ref: 123…



No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s