# What is Pass By Reference and Pass By Value in PHP?

Object Oriented ProgrammingPHPProgramming

Now, let’s understand these two concepts in detail.

In PHP generally, we followed to pass the arguments to the function with passed by value approach. We are following this practice because if the value of the argument within the function is changed, it does not get changed outside of the function.

In some cases we may need to modify function arguments, So to allow a function to modify its arguments, they must be passed by reference.

Let's begin with passed by reference. As it is already mentioned we can pass a variable by reference to a function so the function can modify the variable. To begin the process of passing the parameters passed by reference, prepend an ampersand (&) to the argument name in the function definition.

## Example

Let's test this with a simple example.

<?php
function calculate(&$a){$a++;
}
$a=5; calculate($a);

## Example

Let's test an example to understand pass by value.

<?php
function calculate($a){$a++;
echo $a."<br/>"; }$a=5;
calculate($a); echo$a;
?>

## Output

6
5

## Explanation

Here we have passed the value to the function calculate() as pass by value. Its value gets changed inside the function but that is not reflected outside the function. The value of the variable remains the same outside the function.

Published on 19-Jun-2019 11:27:02