Pass argument by reference

pass argument by reference:

In PHP You Used To Be Able To Preface A Variable With The & Symbol To Tell The Parser To Pass A Reference To The Variable, Not The Variable’s Value. This Granted A Function Access To The Variable

syntax_
function()
{
//code..
}



For example:
<?php
$a1="WILLIAM";
$a2="henry";
$a3="gatES"; 
echo $a1." ".$a2." ".$a3."<br>";
fix_names($a1,$a2,$a3);
echo $a1." ".$a2." ".$a3;
function fix_names(&$n1,&$n2,&$n3)
{
$n1=ucfirst(strtolower($n1));
$n2=ucfirst(strtolower($n2));
$n3=ucfirst(strtolower($n3));
}
?>

OUTPUT:
WILLIAM henry gatES
William Henry Gates

Rather than passing strings directly to the function, you first assign them to variables and print them out to see their “before” values. Then you call the function as before but put a & symbol in front of each parameter, which tells PHP to pass the variables reference only.

Translate »