How do these 2 variables get utilised in the code?:
$run1 & $run2
I was told they store the functions results, however the echo command doesn't utilise them. Is it because the code uses "assign by reference" that they need to be there ? or what is the ultimate purpose of these
function f1(&$array_para)
{
$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"];
CodePudding user response:
they are not utilised as now, only populated.
If you want to see the output, add this under the first echo
print_r($run1);
print_r($run2);
I use "print_r" cause it will output the whole array, echo will print "Array()".
If you want to see "pretty print" from the echo, use that :
echo '<pre>';
print_r($run1);
echo '</pre>';
the "pre" will format the output in a easier way to read! :)
CodePudding user response:
f1 does not return anything, the return statement is missing. f2 does return the result of f1 that is none. because f1 does not return anything.
f1 is being used to pass the value via reference that means that the change is on the actual array $array_param["a"] not on the return of the function. This will modify the original array with the desired value.
If you add the return on f1
function f1(&$array_para)
{
$array_param["a"] = "changed";
return $array_param;
}
This will fix it and you will have 2 places with the same value on variables. for example after running f1:
$arr1 = ["a" => "Tadpole"]; // equals changed
$run1 = f1($arr1); // equals changed
Everything depends on how you want to code, but it does not work because of the missing return.
happy coding.