|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2000-05-30 20:29 UTC] stas at cvs dot php dot net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Dec 22 19:00:01 2025 UTC |
I am trying to pass by reference from a global variable to another global variable inside of a function. From what I can tell it does not work... however, if I try it outside of a function it works just fine. Is this by design? Or am I doing something wrong? Here is an example of what works and what doesn't. <doesnotwork> <?php function test() { global $current; global $parent; if (!is_array($current)) { print("** inside if **<br>"); $parent[0] = array(); $current = &$parent[0]; } else { print("** inside else **<br>"); $current[0] = array(); $current = &$current[0]; } } for ($i = 0; $i < 2; $i++) { test(); } $current[0] = "me"; print("Current[0]=".$current[0]."<br>"); print("Parent[0][0][0]=".$parent[0][0][0]."<br>"); ?> result is ** inside if ** ** inside if ** Current[0]=me Parent[0][0][0]= </doesnotwork> <doeswork> <?php for ($i=0; $i < 2; $i++) { if (!is_array($current)) { print("** inside if **<br>"); $parent[0] = array(); $current = &$parent[0]; } else { $current[0] = array(); $current = &$current[0]; } } $current[0] = "me"; print("Current[0]=".$current[0]."<br>"); print("Parent[0][0][0]=".$parent[0][0][0]."<br>"); ?> ** inside if ** ** inside else ** Current[0]=me Parent[0][0][0]=me </doeswork>