Jump to content

Setting a variable inside a function and making it global in another function..?


Recommended Posts

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

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 );
}

 


 

 

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.

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;

 

 

 

 

 

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.