Just confused here

You must select a tag to post in this category. Please find the tag relating to the section of the course you are on E.g. loops, learn-compatibility

`function f1(&$array_param)
{
$array_param[“a”] = “changed”;
}

function f2($array_param)
{
$return_arr = f1($array_param);
return $return_arr;
}

$arr1 = [“a” => “Tadpole”];
$arr2 = [“a” => “Lily”];

$run1 = f1($arr1);
$run2 = f2($arr2);

echo $arr1[“a”] . " " . $arr2[“a”];`

//the answer is changed Lily:: but i don’t get it cos when printing, the code doesn’t use $run1 variable. So why does it change. Any clarity on that folks. Thanks alot

1 Like

Hello @object3063927502, welcome to the forums! In PHP, the & sign in front of the parameter means that the function actually directly changes the object passed into it, rather than making a copy of that object, and using the copy.

Analogy

The difference would be similar to if I went into a cake shop. If I went in with my own cake, and just wanted it icing, they could take the cake and ice it for me. This would be analogous to using the & before the parameter name.

Not using the & would be like me walking into a cake shop with a cake, but asking the shop to make another cake exactly like my one, then ice it.