Demonic Posted March 11, 2008 Share Posted March 11, 2008 Setting a variable inside a function and making it global inside an inner function doesn't work? Right well I have created this function: function getForumChildrenTree( $id ) { $id = intval( $id ); $treeResult = array( 'topics' => array(), 'posts' => array(), 'forums' => array(), ); applyForumChildrenTree( $id ); } What this function does: Basically this function is set to an id of a forum id, then theres a variable that stores an array. Then theres a recursive function called "applyForumChildrenTree" to update the "treeResult" variable, but it's not exactly working. For some reason when I did print_r inside the "applyForumChildrenTree" the array is as applied: Array ( [forums] => Array ( [0] => 3 [1] => 8 [2] => 5 ) [topics] => Array ( [0] => 5 ) [posts] => Array ( [0] => 5 ) ) Then inside the actual function "getForumChildrenTree" I tried outputting the "treeResult" array using "print_r" after the recursive function "applyForumChildrenTree" as follows: function getForumChildrenTree( $id ) { $id = intval( $id ); $treeResult = array( 'topics' => array(), 'posts' => array(), 'forums' => array(), ); applyForumChildrenTree( $id ); print_r($treeResult); } and it showed me a blank array, (default array I created) as follows: Array ( [topics] => Array ( ) [posts] => Array ( ) [forums] => Array ( ) ) Is this a bug? I'm using PHP 5.2.4 Quote Link to comment https://forums.phpfreaks.com/topic/95711-setting-a-variable-inside-a-function-and-making-it-global-in-another-function/ Share on other sites More sharing options...
Psycho Posted March 12, 2008 Share Posted March 12, 2008 No it's not a bug. Just because you call a function from within another function does not mean variables are "accessible" between the two (i.e. they do not have the same scope). Try this. Modify the function applyForumChildrenTree() to return the array. Then change the getForumChildrenTree() as follows: {code]function getForumChildrenTree( $id ) { $id = intval( $id ); $treeResult = applyForumChildrenTree( $id ); } Quote Link to comment https://forums.phpfreaks.com/topic/95711-setting-a-variable-inside-a-function-and-making-it-global-in-another-function/#findComment-490074 Share on other sites More sharing options...
Demonic Posted March 12, 2008 Author Share Posted March 12, 2008 No it's not a bug. Just because you call a function from within another function does not mean variables are "accessible" between the two (i.e. they do not have the same scope). Try this. Modify the function applyForumChildrenTree() to return the array. Then change the getForumChildrenTree() as follows: function getForumChildrenTree( $id ) { $id = intval( $id ); $treeResult = applyForumChildrenTree( $id ); } I can't do that because I simply said after the recursive function "applyForumChildrenTree" "applyForumChildrenTree" is a recursive function, how the function works is it starts from the root parent, then gets all topics and replies from that forum, then runs that function again based on the current child forum that had the parent from the start and so on.. it keeps doing the process over and over. But I'm going to use $GLOBALS I think. Quote Link to comment https://forums.phpfreaks.com/topic/95711-setting-a-variable-inside-a-function-and-making-it-global-in-another-function/#findComment-490162 Share on other sites More sharing options...
laffin Posted March 12, 2008 Share Posted March 12, 2008 I think what u mean to do is pass a variable by reference, instead of it's value. so that the function can alter the variable function add(&$x,$y) { $x+=y; } $x=0; add($x,15); add($x,5); echo $x; Quote Link to comment https://forums.phpfreaks.com/topic/95711-setting-a-variable-inside-a-function-and-making-it-global-in-another-function/#findComment-490178 Share on other sites More sharing options...
Psycho Posted March 12, 2008 Share Posted March 12, 2008 I can't do that because I simply said after the recursive function "applyForumChildrenTree" "applyForumChildrenTree" is a recursive function, how the function works is it starts from the root parent, then gets all topics and replies from that forum, then runs that function again based on the current child forum that had the parent from the start and so on.. it keeps doing the process over and over. But I'm going to use $GLOBALS I think. Well you *could* do that. You could simply modify applyForumChildrenTree() to accept a parameter that is the input array and it returns the output array. So, when it goes through it's recursive process it adds to the array and in the end it passes back the completed array. But, I think laffin has a better solution anyway. Quote Link to comment https://forums.phpfreaks.com/topic/95711-setting-a-variable-inside-a-function-and-making-it-global-in-another-function/#findComment-490240 Share on other sites More sharing options...
laffin Posted March 12, 2008 Share Posted March 12, 2008 well different ways to do the same thing kinda question. but its good to explore the different ways. Quote Link to comment https://forums.phpfreaks.com/topic/95711-setting-a-variable-inside-a-function-and-making-it-global-in-another-function/#findComment-490289 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.