Setting a variable inside a function and making it global inside an inner function doesn't work?

Setting a variable inside a function and making it global inside an inner function doesn't work?

Post by php4foo » Thu, 13 Mar 2008 08:24:35


Setting a variable inside a function and making it global inside an
inner function doesn't work?

Right well I have created this function:

[code]
function getForumChildrenTree( $id )
{
$id = intval( $id );
$treeResult = array(
'topics' => array(),
'posts' => array(),
'forums' => array(),
);
applyForumChildrenTree( $id );
}
[/code]

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:

[quote]
Array
(
[forums] => Array
(
[0] => 3
[1] => 8
[2] => 5
)

[topics] => Array
(
[0] => 5
)

[posts] => Array
(
[0] => 5
)

)
[/quote]

Then inside the actual function "getForumChildrenTree" I tried
outputting the "treeResult" array using "print_r" after the recursive
function "applyForumChildrenTree" as follows:

[code]
function getForumChildrenTree( $id )
{
$id = intval( $id );
$treeResult = array(
'topics' => array(),
'posts' => array(),
'forums' => array(),
);
applyForumChildrenTree( $id );
print_r($treeResult);
}
[/code]

and it showed me a blank array, (default array I created) as follows:

[quote]
Array
(
[topics] => Array
(
)

[posts] => Array
(
)

[forums] => Array
(
)

)
[/quote]

Is this a bug? I'm using PHP 5.2.4