0

I'm using PHP 7.1.11

Consider the first code snippet with assigning NULL to the reference :

<?php
  $a = 1;
  $b =& $a;

  var_dump($a);
  echo "<br>";
  var_dump($b);
  echo "<br>";

  $a = NULL;

  var_dump($a);
  echo "<br>";
  var_dump($b);
?>

Output of above program is as below :

int(1)
int(1)
NULL
NULL 

Consider second code snippet destroying the reference using unset language construct:

<?php
  $a = 1;
  $b =& $a;

  var_dump($a);
  echo "<br>";
  var_dump($b);
  echo "<br>";

  unset($a); 

  var_dump($a);
  echo "<br>";
  var_dump($b);
?>

Output of above program is as below :

int(1)
int(1)

Notice: Undefined variable: a 
NULL
int(1) 

As per my knowledge a reference is an alias, which allows two different variables to write to the same value. In other words, references are means to access the same variable content by different names.

So, it's expected to change the content of both the variables if one of the variables contains reference of another variable.

The above concept works well when I assign NULL value to the reference but not working when I unset the reference.

  1. Isn't the thing setting variable value to NULL and unsetting the variable i.e. destroying the variable using unset have different meanings in PHP?
  2. What's the exact reason behind this weird behavior?
  3. How these two processes are different?
  4. Why they are producing different outputs?
PHPLover
  • 1
  • 51
  • 158
  • 311
  • 1
    Setting a variable ($a) that is a referenced by another variable ($b) to null is assigning a new value of null to `$a` and hence to `$b` (because it is still a reference to `$a`) – Mark Baker Nov 22 '17 at 17:44
  • 1
    Unsetting the variable `$a`, actually removes the variable from existence, but leaves the hanging reference (`$b`) pointing to the original value of `$a` that remains in memory – Mark Baker Nov 22 '17 at 17:45
  • 1
    The difference is that in the former case the variable will still exist in the scope it was set in, but have a value of null, whereas in the latter case the variable will no longer exist in the scope. – GordonM Nov 22 '17 at 17:46
  • @MarkBaker if `$a` is removed from existence, how can `$b` point to it's value? Are you saying that when a variable is destroyed using `unset()`, it's values remain in memory? – whitwhoa Nov 22 '17 at 17:55
  • If there are other references to the same value, then the value is kept in memory. PHP's garbage collection is based on reference counting, so until the last reference to a value is removed (or falls out of scope), it stays where it is. `unset($a)` doesn't affect `$b` at all, even if they're referencing the same value. – iainn Nov 22 '17 at 17:57
  • The zval_value remains, and that's the value pointed to by `$b` and referenced by `$b`; the zval_struct that was `$a` and by reference by $b is removed.... it might be worth your while reading about [PHP's internal representation of variables](https://nikic.github.io/2015/05/05/Internal-value-representation-in-PHP-7-part-1.html) if you want more detail – Mark Baker Nov 22 '17 at 18:04

0 Answers0