Pass by reference in PHP

Reading Time: < 1 minutes

In PHP you can pass a variable to a function as reference. When you do that actual “Memory address is passed” instead of “A Copy of the value” hence any change you make inside the function will reflect even after the outside after the function is executed.

Note:

You give & (ampersand) before the variable name in the function signature only and not in the function call.

<?php

function change( &$fruit ){
	$fruit = 'Guva';
}

$fruit = 'apple';
echo 'Before call: ' . $fruit;
change( $fruit );
echo 'After call : ' . $fruit;