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?

Dani AI

Generated

Short summary tied to the thread: saw the mysqli connection behave as a single shared instance; and were on the right track. Below are concise, practical details and quick tests to make the difference clear and avoid surprises.

PHP internals (practical view)

  • Scalars use copy-on-write: two variables can share the same internal value until one of them is modified, at which point PHP makes an actual copy. This is an optimization, not pointer-aliasing in the usual language sense.
  • Objects are represented by an object handle inside the variable. Assigning an object copies the handle, so both variables refer to the same underlying instance (that is why closing a mysqli connection through one variable affects the other).

Quick checks and ways to get separate instances

  • To inspect sharing for scalars, use debug helpers (for example debug_zval_dump or Xdebug equivalents) to see refcounts before and after mutation.
  • To check object identity use identity checks and object IDs; to create an independent object use clone or instantiate a new object (for DB connections, open a new connection rather than cloning).

Example checks:

var_dump($objA === $objB); // true if same instance
$objB = clone $objA;
var_dump($objA === $objB); // false after clone

Notes and cautions

  • The reference operator & creates an alias at the variable level (two names for the same zval) and is rarely needed; it is different from object-handle semantics.
  • For mysqli-style resources, prefer explicit new connections or cloning patterns documented for the extension rather than relying on variable copying.
  • Behavior is stable from a user-facing perspective, though internals changed across PHP versions; relying on explicit checks (===, debug_zval_dump, spl_object_id/spl_object_hash) gives definitive answers.

Recommended Answers

All 5 Replies

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.

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.

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 :)

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.