php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #31956 array passed by reference is modified but should'nt be
Submitted: 2005-02-13 17:27 UTC Modified: 2005-02-14 08:17 UTC
From: pillepop2003 at yahoo dot de Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 4CVS, 5CVS (2005-02-13) OS: *
Private report: No CVE-ID: None
 [2005-02-13 17:27 UTC] pillepop2003 at yahoo dot de
Description:
------------
Bug #26030 realtes to the same problem and should be reopened: Why? It seems to be *NOT* related to #25996, as formerly reported by bugmoriyoshi@php.net, which was the reason to close it.

Problem:
If a part of an array, that does not yet exist, is passed to a function by reference, this part will be created automatically and filled with NULL.

As long as no there is no function implemented to delete the NULL entry (unset does not work), this behavior cannot be correct.

Please see my example - it clearly states that the behavior is incorrect.

Reproduce code:
---------------
See why:
The following behavior *cannot* be intentional:

<?php
	function func(&$a)
	{
		// void();
	}
	
	$a['one'] =1;
	func($a['two']);
?>	

var_dump($a) returns

	array(2) {
		["one"]=>
		int(1)
		["two"]=>
		NULL
	}

Expected result:
----------------
array(2) {
		["one"]=>
		int(1)
	}

Actual result:
--------------
array(2) {
		["one"]=>
		int(1)
		["two"]=>
		NULL
	}

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-02-13 17:57 UTC] sniper@php.net
This happens with both PHP_4_3 and HEAD from today.
(only verified that it happens. I don't know if it's intentional or not :)

 [2005-02-13 18:14 UTC] sniper@php.net
What would you expect this to output:

<?php

function func1(&$a)
{
    // void();
}

var_dump($c);
func1($c);
var_dump($c);

?>  

IMO, this is exactly what you'd expect. The non-existing variable (or in your example, non-existing array index) gets 
value of NULL. I don't see what else should happen..


 [2005-02-13 20:29 UTC] pillepop2003 at yahoo dot de
<?php

function func1(&$a)
{
    // void();
}

var_dump($c);
func1($c);
var_dump($c);

?> 

should IMO output

array(2) {
		["one"]=>
		int(1)
	}
array(2) {
		["one"]=>
		int(1)
	}

Why should the non-existing variable get a value (of NULL) - this is a paradoxon, isn't it? Can't it just be "not mentioned"?
 [2005-02-13 22:49 UTC] sniper@php.net
Don't mix my example with yours, mine is passing something else than an array index to the function. And this is actually (kind of) documented as undefined:

http://www.php.net/manual/en/language.references.pass.php

See the last paragraph, about what can be passed by reference:

"Any other expression should not be passed by reference, as the result is undefined."

 [2005-02-14 08:13 UTC] pillepop2003 at yahoo dot de
okay, you seem to be right.
in my test unset() did not work correctly (for whatever reason...) in the beginning, what made it hard to believe that a variable is set to NULL and can't be unset.

sorry for wasting your time.
Keep up thw good work.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 16 16:01:34 2025 UTC