PHP variable variable puzzle

execute the 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" } } 

execute the 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 reassigned the value of $ a, why do RECURSION circular references exist?

+6
source share
1 answer

To remove a link, you need to call unset . Without unset after $a = array('yes'); $a is still limited to $b , and they are still links. Thus, the second part has the same behavior as the first.

Note, however, that references inside arrays are potentially dangerous. Performing a normal (not by reference) assignment using a link on the right side does not turn the left side into a link, but links inside arrays are stored in these normal tasks.

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

+3
source

Source: https://habr.com/ru/post/978075/


All Articles