views:

81

answers:

5

This is the test and the response I get. I think this could be problematic and should throw an error or a notice but I cannot understand why is tolerated.

<?php
    $test = array( 0 => 'test', 1=> &$test );
    var_dump( $test );

    // array(2) { [0]=> string(4) "test" [1]=> &array(2) { [0]=> string(4) "test" [1]=> &array(2) { [0]=> string(4) "test" [1]=> *RECURSION* } } }
?>
+1  A: 

I would guess that detecting such a loop is non-trivial, and would be immediately apparent at runtime if the behaviour was incorrect.

Visage
+1  A: 

Why is it problematic? PHP is smart enough to identify that an array is being recursively called.

The same happens if you print_r($GLOBALS), I see no harm in this.

Alix Axel
+1  A: 

You're setting a reference, that is, a pointer so there is no true recursion, no loop. So no, it shouldn't throw an error.

Andy E
A: 

Actualy the *RECURSION* message is a error message, which ends the script execution. Else it would execute it till the memory limit is reaced.

streetparade
+1  A: 

It is true recursion, and *RECURSION* is not a real error message. It's not problematic, because $test is not actively recurring, and in this case var_dump is smart enough to stop before exhausting memory.

Andrea