-9

What is the difference between in php function

  • A parameter passage by variable
  • A parameter pass by reference?
2
  • 3
    I think you mean by value not by variable. This is also covered all over the place. At least try to look first. Commented Mar 22, 2018 at 10:21
  • 2
    please read this stackoverflow.com/help/how-to-ask Commented Mar 22, 2018 at 10:22

2 Answers 2

1

The best way to understand it is from an example:

function foo($a) { $a = 123; echo "Value in function: " . $a; } function bar(&$a) { $a = 123; echo "Value in function: " . $a; } $var = 555; foo($var); echo "After foo: " . $var; bar($var); echo "After bar: " . $var; 

Basically you will change the value pointed by the reference, changing it also out of the function scope, while in a normal by-value when the function is finished the changes made to the variable will be lost. Here is an official PHP manual link, with more examples.

Sign up to request clarification or add additional context in comments.

Comments

-1

A parameter passage by value - value of a variable is passed.

$b = 1; function a($c) { $c = 2; // modifying this doesn't change the value of $b, since only the value was passed to $c. } a($b); echo $b; // still outputs 1 

A parameter pass by reference? - a pointer to a variable is passed.

$b = 1; function a($c) { $c = 2; // modifying this also changes the value of $b, since variable $c points to it } a($b); echo $b; // outputs 2 now after calling a(); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.