Web EndMemo


PHP Pass by Reference


PHP functions can use reference as parameters. References are address points of the variables. For example, if an array of 1000 elements passed as a function parameter, the array may by several kb in size that needed to be passed over, however its reference do not have this disadvantage.

The variables passed by reference can be modified inside the function, and the modified value will be returned. Parameters passed as reference should add a "&" sign in the front during the function definition, and do not need such sign when used.

<?PHP
	function addno(&$x)
	{
		$x = "No. " . $x;
	}
	var $n = 324;
	addno($n);
	echo "$n";  //No. 324
?>

Pass by reference is very important when there are several values to be modified inside the function.

<?PHP
	function addno(&$x,&$y)
	{
		$x = "No. " . $x;
		$y += 12;
	}
	$n = 324;
	$m = 20;
	addno($n,$m);
	echo "$n";  //No. 324
	echo "$m";  //32
?>