6

run following code:

<?php
$a = array('yes');
$a[] = $a;
var_dump($a);

out put:

array(2) {
  [0]=>
  string(3) "yes"
  [1]=>
  array(1) {
    [0]=>
    string(3) "yes"
  }
}

run following code:

<?php
$a = array('no');
$b = &$a;
$a[] = $b;
$a = array('yes');
$a[] = $a;
var_dump($a);

out put:

array(2) {
  [0]=>
  string(3) "yes"
  [1]=>
  array(2) {
    [0]=>
    string(3) "yes"
    [1]=>
    *RECURSION*
  }
}

I have reassigned value of $a, why there are RECURSION circular references?

1
  • 1
    Wow, something interesting on stackoverflow! :). +1!!! Commented Nov 12, 2014 at 7:59

1 Answer 1

3

To remove reference you need to call unset. Without unset after $a = array('yes'); $a still bounds with $b and they are still references. So the second part has the same behavior as first one.

Note, however, that references inside arrays are potentially dangerous. Doing a normal (not by reference) assignment with a reference on the right side does not turn the left side into a reference, but references inside arrays are preserved in these normal assignments.

http://php.net/manual/en/language.references.whatdo.php

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.