In PHP, you can create a reference to a variable using the & operator. A reference means that two variables point to the same value, so changing one variable can also affect the other.

Here is an example of creating reference in PHP. In the code below $b is a reference to $a, means if we modify $b, $a also will take effect.

PHP
$a = ['foo'];
$b = &$a;
$b[] = 'bar';
var_dump($a)  # result will be ['foo','bar']

This happens because $a and $b are references to the same underlying value. Modifying the value through either variable affects the other.

# When Should You Use References?

PHP references can be useful when you intentionally want multiple variables to work with the same value, particularly when passing variables to functions or modifying data directly.

However, references can make code harder to understand if used unnecessarily. In most PHP applications, including Laravel projects, you should use them only when their behavior is actually needed.

# A Practice for you

consider we have an array of numbers. how to multiple all of the array values by 2 without defining a new variable? write the answer in the comments.

PHP
$numbers = [1,5,10,11];
foreach($numbers as ...){
   // ...
}
# $numbers should be equal to [2,10,20,22]