skahan
8/14/2013 - 11:23 PM

PHP Pass By Reference Example

PHP Pass By Reference Example

Normally, when you pass a variable to a function, the function receives a copy of that variable's value. 
By passing a reference to a variable, however, the function can refer to — and, more importantly, modify — 
the original variable. So, use pass-by-reference whenever you want a function to change a variable that's passed to it.

<?php
function ref_test($var){
  $var = $var*2;	
}
$a = 10;
ref_test($a);
echo $a;//10

function ref_test2(&$var){
	$var = $var*2;	
}
$a = 10;
ref_test2($a);
echo $a;//20

function ref_test3(){
	GLOBAL $a
	$a = $a*2;	
}
$a = 10;
ref_test3();
echo $a;//20
?>

# ref: http://bit.ly/2siTg4H