0

If i assign a value to a variable like this in PHP:

$var1 = 123;

and then assign another value to another variable like this:

$var2 = $var1;

Are those two variables pointing to the same value in the memory (like in Java if I'm not mistaken) or are they two copies of the same value?

3
Contributors
5
Replies
6
Views
6 Years
Discussion Span
Last Post by gunnarflax
0

I *think* in this case they will be two copies of the same value. You could probably check for definite if you use the PHP debugger in Linux CLI.

0

As long as $var1 and $var2 are primitive types and not objects, then $var2 is not a pointer to $var1 (however, I have read that PHP will store it internally with a pointer until the value of $var2 is changed to something else.)

If you need $var2 to be a reference to $var1 you can do this:

$var2 = & $var1;

Objects are always passed by reference (in PHP 5.x.) Trying to pass an object by reference produces in error in php 5.x.

Edited by madCoder: n/a

0

are you sure about this? I have code snippet with a mysqli object in $var1 and I have put $var2 equal to $var1. I can perform query's through both variables but If i close the connection on of them the other is also disconnected and I get an error while performing a query. Wouldn't that mean that the variables are pointers and that the language is built with the "one memory slot, several pointers"-way?

What exactly is the use of "&"? Thank you for taking your time :)

1

are you sure about this? I have code snippet with a mysqli object in $var1 and I have put $var2 equal to $var1. I can perform query's through both variables but If i close the connection on of them the other is also disconnected and I get an error while performing a query. Wouldn't that mean that the variables are pointers and that the language is built with the "one memory slot, several pointers"-way?

What exactly is the use of "&"? Thank you for taking your time :)

Yes, as you stated, $var1 was an object so assigning $var1 to $var2 simply made $var2 a reference to $var1. So yes, $var2 is basically a pointer to the same object.

The & reference operator is normally used in a function declaration to have a parameter "pass by reference." This lets a function directly modify one of its parameters.

function example(& $p1, $p2)
{
   $p1 += 10;  // p1 is a reference to $a and modifies the variable $a.
   return $p1+$p2;
}

$a = 5;
$b = 2;
$sumplus10 = example($a, $b);

echo $a.','.$b.', '.$sumplus10;
// $a now equals 15 instead of 5.

This is just a quick (and rather useless example) of how pass by reference works.

To be honest I don't use it very much since it makes code harder to debug. Also, php already uses some optimization tricks to make unnecessary to do this for performance reasons.

The manual explains it really well here: PHP: References Explained

0

Thank you very much for your elaborate answer madCoder! It's so appreciated when people take time and explain things thoroughly :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.