Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

What's the key deference between passing by reference and passing by value in PHP ?

function ( &$var1 ,$var2)
{
      var1[$x]...     
}

Function that passes an array by reference and value, what would the impact be.

share|improve this question

closed as unclear what you're asking by gnat, GlenH7, Bart van Ingen Schenau, MichaelT, Robert Harvey Jul 9 at 22:18

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

4  
Did you read the manual? Is there something in there that's confusing you? –  Yannis Rizos Aug 26 '12 at 21:52

1 Answer 1

It's a question of scope; from the manual:

You can pass a variable by reference to a function so the function can modify the variable.

For instance:

$outer_var = "Hello";

function nullify_by_value($inner_var)
{
  $inner_var = null;
}

nullify_by_value($outer_var);
var_dump($outer_var);

This will output string 'Hello' (length=5) because the function can not modify the variable passed to it by value (as passing by value creates a copy of the variable in the function's local scope).

function nullify_by_reference(&$inner_var)
{
  $inner_var = null;
}

nullify_by_reference($outer_var);
var_dump($outer_var);

This will output null because the function has modified the variable which was passed to it by reference (i.e., outside the function's local scope).

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.