What is pass by reference in php?

PHP variables, by default, are passed by value. Once PHP variables are passed by value, the variable scope, defined at the function level is linked within the scope of the function. Modification of either of the variables has no impact.

Let’s see an example:

<?php

// Function used for assigning new 
// value to $string variable and 
// printing it 
function print_string($string)
{
  $string = "W3docs" . "\n";
  
  // Print $string variable 
  print($string);
}

// Driver code 
$string = "Global w3docs" . "\n";
print_string($string);
print($string);
?>

The output will look as follows:

  Function w3docs
  Global w3docs

For passing variables by reference, it is necessary to add the ampersand (&) symbol before the argument of the variable.

An example of such a function will look as follows: function( &$x ).

The scope of the global and function variables becomes global. The reason is that they are defined by the same reference. Hence, after changing the global variable, the variable that is inside the function also gets changed.

Here is a clear example:

<?php

// Function applied to assign a new value to 
// $string variable and printing it 
function print_string(&$string)
{
  
  $string = "Function w3docs \n";
  
  // Print $string variable 
  print($string);
}

// Driver code 
$string = "Global w3docs \n";
print_string($string);
print($string);
?>

The output will be:

  Function w3docs 
  Function w3docs

As a rule, PHP variables start with the $ sign followed by the variable name.

While assigning a text value to a variable, putting quotes around the value is necessary.

In contrast to other programming languages, PHP doesn’t have a command to declare a variable. It is generated at the moment a value is first assigned to it.

In PHP, variables are containers to store data.

The PHP variables may have both short( for instance, x and y) and more descriptive names (fruitname, age, height, and so on).

When it comes to references, things get the tiniest bit more complicated - you need to be able to accept parameters by reference and also return values by reference. This is done with the reference operator, &.

Marking a variable as "passed by reference" is done in the function definition, not in the function call. That is:

function multiply(&$num1, &$num2) {

is correct, whereas

$mynum = multiply(&5, &10);

is wrong. This means that if you have a function being used hundreds (thousands?) of times across your project, you only need edit the function definition to make it take variables by reference. Passing by reference is often a good way to make your script shorter and easier to read - the choice is not always driven by performance considerations. Consider this code:

<?php
    function square1($number) {
        return $number * $number;
    }

    $val = square1($val);

    function square2(&$number) {
        $number = $number * $number;
    }

    square2($val);
?>

The first example passes a copy of $val in, multiplies the copy, then returns the result which is then copied back into $val. The second example passes $val in by reference, and it is modified directly inside the function - hence why square2($val); is all that is required in place of the first example's copying.

One key thing to remember is that a reference is a reference to a variable . If you define a function as accepting a reference to a variable, you cannot pass a constant into it. That is, given our definition of square2(), you cannot call the function using square2(10);, as 10 is not a variable, so it cannot be treated as a reference.

Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: Returning by reference >>

Previous chapter: Parameters

Jump to:

Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.

What is pass by value and pass by reference in PHP?

By default, function arguments are passed by value (so that if the value of the argument within the function is changed, it does not get changed outside of the function). To allow a function to modify its arguments, they must be passed by reference.

Is PHP object pass by reference?

In PHP, objects are passed by references by default. Here, reference is an alias, which allows two different variables to write to the same value. An object variable doesn't contain the object itself as value. It only contains an object identifier which allows using which the actual object is found.

Is PHP array pass by reference?

With regards to your first question, the array is passed by reference UNLESS it is modified within the method / function you're calling. If you attempt to modify the array within the method / function, a copy of it is made first, and then only the copy is modified.

Why do we pass by reference?

If you recall, using pass by reference allows us to effectively “pass” the reference of a variable in the calling function to whatever is in the function being called. The called function gets the ability to modify the value of the argument by passing in its reference.