php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44128 Incorrect recursion detection for undefined variable
Submitted: 2008-02-15 12:48 UTC Modified: 2008-02-20 14:26 UTC
From: felipensp at gmail dot com Assigned: dmitry (profile)
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.3CVS-2008-02-15 (CVS) OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: felipensp at gmail dot com
New email:
PHP Version: OS:

 

 [2008-02-15 12:48 UTC] felipensp at gmail dot com
Description:
------------
Undefined variables non-reference/reference as item of an array has different behavior.

Reproduce code:
---------------
1:
felipe@felipe:~/php5$ sapi/cli/php -r 'var_dump($a =array($a));'

2:
felipe@felipe:~/php5$ sapi/cli/php -r 'var_dump(array(&$a));'

3:
felipe@felipe:~/php5$ sapi/cli/php -r 'var_dump($a =array(&$a));'

Expected result:
----------------
2 and 3 as same result.

Actual result:
--------------
1:
array(1) {
  [0]=>
  NULL
}

2:
array(1) {
  [0]=>
  &NULL
}

3:
array(1) {
  [0]=>
  &array(1) {
    [0]=>
    &array(1) {
      [0]=>
      *RECURSION*
    }
  }
}

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-02-20 14:26 UTC] dmitry@php.net
Your expectation is wrong:

In your second example two variables refer to the same NULL value. (first element of array and variable $a)

Compare the output of the following two scripts

<?php
$x = array(&$a);
var_dump($x);
?>
Actual result:
--------------
array(1) {
  [0]=>
  &NULL
}

<?php
$x = array(&$a);
unset($a);
var_dump($x);
?>
Actual result:
--------------
array(1) {
  [0]=>
  NULL
}

In the third example you assign the array from second example to $a, but $a is a reference, so the assignment will update the first element of array too.

<?php
$b = array(&$a);
var_dump($b);
$a = $b;
var_dump($b);
?>
Actual result:
--------------
array(1) {
  [0]=>
  &NULL
}
array(1) {
  [0]=>
  &array(1) {
    [0]=>
    &array(1) {
      [0]=>
      *RECURSION*
    }
  }
}


All results are the same as they should be.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 17 00:01:31 2025 UTC