7

(1) I want to know what is the difference between call by value and call by reference in php. PHP works on call by value or call by reference?

(2) And also i want to know that do you mean by $$ sign in php

For example:-

$a = 'name';
$$a = "Paul";
echo $name; 

output is Paul

As above example what do u mean by $$ on PHP.

hakre
  • 193,403
  • 52
  • 435
  • 836
poter
  • 73
  • 1
  • 1
  • 4
  • 2
    See http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php and http://stackoverflow.com/search?q=%5Bphp%5D+difference+call+by+reference+value – deceze Jun 20 '11 at 04:50

5 Answers5

13

$$a = b; in PHP means "take the value of $a, and set the variable whose name is that value to equal b".

In other words:

$foo = "bar";
$$foo = "baz";
echo $bar; // outputs 'baz'

But yeah, take a look at the PHP symbol reference.

As for call by value/reference - the primary difference between the two is whether or not you're able to modify the original items that were used to call the function. See:

function increment_value($y) {
    $y++;
    echo $y;
}

function increment_reference(&$y) {
    $y++;
    echo $y;
}

$x = 1;
increment_value($x); // prints '2'
echo $x; // prints '1'
increment_reference($x); // prints '2'
echo $x; // prints '2'

Notice how the value of $x isn't changed by increment_value(), but is changed by increment_reference().

As demonstrated here, whether call-by-value or call-by-reference is used depends on the definition of the function being called; the default when declaring your own functions is call-by-value (but you can specify call-by-reference via the & sigil).

Community
  • 1
  • 1
Amber
  • 507,862
  • 82
  • 626
  • 550
8

Let's define a function:

function f($a) {
  $a++;
  echo "inside function: " . $a;
}

Now let's try calling it by value(normally we do this):

$x = 1;
f($x);
echo "outside function: " . $x;

//inside function: 2
//outside function: 1

Now let's re-define the function to pass variable by reference:

function f(&$a) {
  $a++;
  echo "inside function: " . $a;
}

and calling it again.

$x = 1;
f($x);
echo "outside function: " . $x;

//inside function: 2
//outside function: 2

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

Yasser Souri
  • 1,967
  • 2
  • 19
  • 26
1

Call by value: Passing the variable value directly and it will not affect any global variable.

Call by reference: Passing the address of a variable and it will affect the variable.

0

Call by value means passing the value directly to a function. The called function uses the value in a local variable; any changes to it do not affect the source variable.

Call by reference means passing the address of a variable where the actual value is stored. The called function uses the value stored in the passed address; any changes to it do affect the source variable.

Ashish Pathak
  • 827
  • 8
  • 16
0

It means $($a), so its the same as $name (Since $a = 'name'). More explanation here What does $$ (dollar dollar or double dollar) mean in PHP?

Community
  • 1
  • 1
looneydoodle
  • 369
  • 6
  • 26
  • ) if both are same then kkk problem solved –difference between call by value and call by reference in php – poter Jun 20 '11 at 05:02