php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #27627 function & fct() create entry in array if key does not exist
Submitted: 2004-03-17 10:29 UTC Modified: 2004-03-17 14:09 UTC
From: jparneodo at yahoo dot fr Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 4.3.5RC3 OS: Red Hat 9.0
Private report: No CVE-ID: None
 [2004-03-17 10:29 UTC] jparneodo at yahoo dot fr
Description:
------------
Returning by reference in a function or method
modify the array if the key asked does not exist

Reproduce code:
---------------
$a=array('x'=>1,'y'=>2);
function & getZ(){
	global $a;
	return $a['z'];
}
function & getT(&$a){
	return $a['t'];
}
function & get4(&$a){
	return $a[4];
}
getZ();
print_r($a);
getT($a);
print_r($a);
get4($a);
print_r($a);


Expected result:
----------------
Array
(
    [x] => 1
    [y] => 2
)
Array
(
    [x] => 1
    [y] => 2
)
Array
(
    [x] => 1
    [y] => 2
)


Actual result:
--------------
Array
(
    [x] => 1
    [y] => 2
    [z] =>     // NULL
)
Array
(
    [x] => 1
    [y] => 2
    [z] =>     // NULL
    [t] =>     // NULL
)
Array
(
    [x] => 1
    [y] => 2
    [z] =>     // NULL
    [t] =>     // NULL
    [4] =>     // NULL
)


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-03-17 14:09 UTC] iliaa@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

When you try to access a non-existant array element you 
effectively create it, hence the NULL entries in the array. 
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Thu Aug 05 02:01:23 2021 UTC