-1

i am learning php and i have to solve an exercise, it is about passing value by reference in php. I have to justify the output, i want you guys to correct my answer.

I submited the code below there are two functions and four variable we pass the value by reference from the SecondFunction to the FirstFunction.

there are five answers or outputs the exercise is to get the right output and explain and justify the answer.

Thank you very much.

<?php
function FirstFunktion(&$param){
    $param = $param * 2;
}
function SecondFunktion($param){
    $param = $param / 2;
}
$var1 = 10;
$var2 = 20;
$var3 = 30;
$var4 = 0;

$var4 = &$var1;
echo "var4: $var4<br />"; // output: var4: 10

FirstFunktion($var4);
echo "var1: $var1<br />"; // output: var1: 20

if ($var4 > $var2){
    $var4 = $var2;
}else{
    $var4 = &$var3;
}
SecondFunktion($var4);

echo "var4: $var4<br />"; // output: var4: 30

FirstFunktion($var3);

echo "var1: $var1<br />"; // output: var1: 20
echo "var2: $var2<br />"; // output: var2: 20
?>

My answers

  1. output: var4: 10 here we have value 10 because we got the value of $var1 = 10 by passing the value by reference $var4 = &$var1

  2. output: var1: 20 here we run $var4 through the FirstFunction that multiply the value of $var4 10 * 2 then we output $var1 the value is the same for both $var1 and $var4

  3. output: var4: 30 here we have an if which is not true then the output is else $var3 and $var4 have now the same value 30 then we run $var4 through SecondFunction 30/2=15 by reference will be automatically changed through the FirstFunction 15*2=30 the final result is 30

  4. output: var1: 20 here is tricky we run the FirstFunktion($var3) but has no effect on $var1 ($var1 remained unchanged and doesn't get the value of $var3 and $var4)

  5. output: var2: 20 here $var2 is always the same and doesn't get any reference as we initialised $var2 = 20

Lelio Faieta
  • 6,457
  • 7
  • 40
  • 74
omar.f
  • 3
  • 1

2 Answers2

0
  1. Your answer is correct. However, in this specific context, it wouldn't have mattered if it wasn't passed by reference.

  2. Your answer is correct. Here, because $var4 is a reference to $var1, anything that happens to the latter affects to the former.

  3. I don't think your answer is correct. The reason why $var4 is 30 is that it's been given the value of $var3 (because of the failed if). The SecondFunktion is a red herring, it does nothing to the value being fed (because the value is not fed by reference).

  4. This is a good one. Your answer is correct, but what the lesson is trying to teach you is the following:

<?php $var_parent = "parent"; $var_child = &$var_parent; var_dump($var_child); //will output parent $var_grandparent = "grandparent"; $var_parent = &$var_grandparent; var_dump($var_child); //will still output parent even tho $var_parent has changed

  1. Your answer is correct. The second variable hasn't been touched.
dearsina
  • 4,774
  • 2
  • 28
  • 34
0

I would like to correct your third answer as follows,

output: var4: 30 here we have an if which is not true then the output is else $var3 and $var4 have now the same value 30 then we run $var4 through SecondFunction but it is a call by value so the values wont change.

Shoyeb Sheikh
  • 2,659
  • 2
  • 10
  • 19
  • You mean here we call the value $var4 = 0 not reference $var4 = &$var3 which is 30 is that right? – omar.f Apr 02 '19 at 10:12
  • `$var4 = &$var3;` after this statement value of $var4 is 30, then a call to `SecondFunktion($var4)` is call by value because it is passed by value ( see the signature of this function i.e `function SecondFunktion($param)` no & with $param that means it a call by value ) , so $var4 remains 30 no change in the value of $var4 – Shoyeb Sheikh Apr 02 '19 at 13:31
  • Thank You very much for your help – omar.f Apr 03 '19 at 10:07