PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > php.general > Setting a variable inside a function and making it global insidean inner function doesn't work?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Setting a variable inside a function and making it global insidean inner function doesn't work?

Réponse
 
LinkBack Outils de la discussion
Vieux 12/03/2008, 00h24   #1
Lamonte
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Setting a variable inside a function and making it global insidean inner function doesn't work?

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

Citation:
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:

Code:
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:

Citation:
Array
(
[topics] => Array
(
)

[posts] => Array
(
)

[forums] => Array
(
)

)
Is this a bug? I'm using PHP 5.2.4
  Réponse avec citation
Vieux 12/03/2008, 02h02   #2
Shawn McKenzie
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Setting a variable inside a function and making it global insideaninner function doesn't work?

Lamonte wrote:
> 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 );
> }
>
>
> 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:
>
>
Citation:
> 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:
>
>
Code:
> 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:
>
>
Citation:
> Array
> (
> [topics] => Array
> (
> )
>
> [posts] => Array
> (
> )
>
> [forums] => Array
> (
> )
>
> )
>
>
> Is this a bug? I'm using PHP 5.2.4


Well you haven't defined anything global in your code, unless you did
inapplyForumChildrenTree(), in which case it needs to be global
everywhere that you want to use it as global.

Easiest way would just be to use it like this everywhere:

$GLOBALS['treeResult'] = array(
'topics' => array(),
'posts' => array(),
'forums' => array(),
);

-Shawn
  Réponse avec citation
Vieux 12/03/2008, 02h09   #3
Shawn McKenzie
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Setting a variable inside a function and making it global insideaninnerfunction doesn't work?

Shawn McKenzie wrote:
> Lamonte wrote:
>> 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 );
>> }
>>
>>
>> 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:
>>
>>
Citation:
>> 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:
>>
>>
Code:
>> 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:
>>
>>
Citation:
>> Array
>> (
>> [topics] => Array
>> (
>> )
>>
>> [posts] => Array
>> (
>> )
>>
>> [forums] => Array
>> (
>> )
>>
>> )
>>
>>
>> Is this a bug? I'm using PHP 5.2.4

>
> Well you haven't defined anything global in your code, unless you did
> inapplyForumChildrenTree(), in which case it needs to be global
> everywhere that you want to use it as global.
>
> Easiest way would just be to use it like this everywhere:
>
> $GLOBALS['treeResult'] = array(
> 'topics' => array(),
> 'posts' => array(),
> 'forums' => array(),
> );
>
> -Shawn


After a second look, probably a better alternate would be to have
applyForumChildrenTree() return the array, then use $treeResult =
applyForumChildrenTree( $id );

-Shawn
  Réponse avec citation
Vieux 12/03/2008, 04h31   #4
Shawn McKenzie
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Re: Setting a variable inside a function and making itglobal insideaninner function doesn't work?

Lamonte wrote:
> Shawn McKenzie wrote:
>> Shawn McKenzie wrote:
>>
>>> Lamonte wrote:
>>>
>>>> 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 );
>>>> }
>>>>
>>>>
>>>> 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:
>>>>
>>>>
Citation:
>>>> 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:
>>>>
>>>>
Code:
>>>> 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:
>>>>
>>>>
Citation:
>>>> Array
>>>> (
>>>> [topics] => Array
>>>> (
>>>> )
>>>>
>>>> [posts] => Array
>>>> (
>>>> )
>>>>
>>>> [forums] => Array
>>>> (
>>>> )
>>>>
>>>> )
>>>>
>>>>
>>>> Is this a bug? I'm using PHP 5.2.4
>>>>
>>> Well you haven't defined anything global in your code, unless you did
>>> inapplyForumChildrenTree(), in which case it needs to be global
>>> everywhere that you want to use it as global.
>>>
>>> Easiest way would just be to use it like this everywhere:
>>>
>>> $GLOBALS['treeResult'] = array(
>>> 'topics' => array(),
>>> 'posts' => array(),
>>> 'forums' => array(),
>>> );
>>>
>>> -Shawn
>>>

>>
>> After a second look, probably a better alternate would be to have
>> applyForumChildrenTree() return the array, then use $treeResult =
>> applyForumChildrenTree( $id );
>>
>> -Shawn
>>
>>

> As I said in my first reply, its a "Recursive function".so I couldn't
> return the array.
>


Please reply to the list.

Sure, your function can return an array if it's recursive. It returns
it to itself and uses whatever logic you need to add to the current
array or merge or remove or whatever. Regardless, I explained why you
aren't seeing the vars as global and a better way to do it using the
$GLOBALS array. I may be off but we would need to see the
applyForumChildrenTree() to know.

-Shawn
  Réponse avec citation
Vieux 12/03/2008, 04h34   #5
Lamonte
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Re: Setting a variable inside a function and making itglobal insideaninner function doesn't work?

Shawn McKenzie wrote:
> Lamonte wrote:
>
>> Shawn McKenzie wrote:
>>
>>> Shawn McKenzie wrote:
>>>
>>>
>>>> Lamonte wrote:
>>>>
>>>>
>>>>> 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 );
>>>>> }
>>>>>
>>>>>
>>>>> 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:
>>>>>
>>>>>
Citation:
>>>>> 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:
>>>>>
>>>>>
Code:
>>>>> 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:
>>>>>
>>>>>
Citation:
>>>>> Array
>>>>> (
>>>>> [topics] => Array
>>>>> (
>>>>> )
>>>>>
>>>>> [posts] => Array
>>>>> (
>>>>> )
>>>>>
>>>>> [forums] => Array
>>>>> (
>>>>> )
>>>>>
>>>>> )
>>>>>
>>>>>
>>>>> Is this a bug? I'm using PHP 5.2.4
>>>>>
>>>>>
>>>> Well you haven't defined anything global in your code, unless you did
>>>> inapplyForumChildrenTree(), in which case it needs to be global
>>>> everywhere that you want to use it as global.
>>>>
>>>> Easiest way would just be to use it like this everywhere:
>>>>
>>>> $GLOBALS['treeResult'] = array(
>>>> 'topics' => array(),
>>>> 'posts' => array(),
>>>> 'forums' => array(),
>>>> );
>>>>
>>>> -Shawn
>>>>
>>>>
>>> After a second look, probably a better alternate would be to have
>>> applyForumChildrenTree() return the array, then use $treeResult =
>>> applyForumChildrenTree( $id );
>>>
>>> -Shawn
>>>
>>>
>>>

>> As I said in my first reply, its a "Recursive function".so I couldn't
>> return the array.
>>
>>

>
> Please reply to the list.
>
> Sure, your function can return an array if it's recursive. It returns
> it to itself and uses whatever logic you need to add to the current
> array or merge or remove or whatever. Regardless, I explained why you
> aren't seeing the vars as global and a better way to do it using the
> $GLOBALS array. I may be off but we would need to see the
> applyForumChildrenTree() to know.
>
> -Shawn
>
>

Yep I'm using globals, thanks .

function getForumChildrenTree( $id )
{
$id = intval( $id );
$GLOBALS['treeResult'] = array(
'topics' => array(),
'posts' => array(),
'forums' => array(),
);
applyForumChildrenTree( $id );
$treeResult = $GLOBALS['treeResult'];
unset($GLOBALS['treeResult']);
return $treeResult;
}
  Réponse avec citation
Vieux 12/03/2008, 04h47   #6
Shawn McKenzie
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Re: Setting a variable inside a function and making itglobalinsideaninner function doesn't work?

Lamonte wrote:
> Shawn McKenzie wrote:
>> Lamonte wrote:
>>
>>> Shawn McKenzie wrote:
>>>
>>>> Shawn McKenzie wrote:
>>>>
>>>>
>>>>> Lamonte wrote:
>>>>>
>>>>>> 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 );
>>>>>> }
>>>>>>
>>>>>>
>>>>>> 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:
>>>>>>
>>>>>>
Citation:
>>>>>> 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:
>>>>>>
>>>>>>
Code:
>>>>>> 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:
>>>>>>
>>>>>>
Citation:
>>>>>> Array
>>>>>> (
>>>>>> [topics] => Array
>>>>>> (
>>>>>> )
>>>>>>
>>>>>> [posts] => Array
>>>>>> (
>>>>>> )
>>>>>>
>>>>>> [forums] => Array
>>>>>> (
>>>>>> )
>>>>>>
>>>>>> )
>>>>>>
>>>>>>
>>>>>> Is this a bug? I'm using PHP 5.2.4
>>>>>>
>>>>> Well you haven't defined anything global in your code, unless you did
>>>>> inapplyForumChildrenTree(), in which case it needs to be global
>>>>> everywhere that you want to use it as global.
>>>>>
>>>>> Easiest way would just be to use it like this everywhere:
>>>>>
>>>>> $GLOBALS['treeResult'] = array(
>>>>> 'topics' => array(),
>>>>> 'posts' => array(),
>>>>> 'forums' => array(),
>>>>> );
>>>>>
>>>>> -Shawn
>>>>>
>>>> After a second look, probably a better alternate would be to have
>>>> applyForumChildrenTree() return the array, then use $treeResult =
>>>> applyForumChildrenTree( $id );
>>>>
>>>> -Shawn
>>>>
>>>>
>>> As I said in my first reply, its a "Recursive function".so I couldn't
>>> return the array.
>>>
>>>

>>
>> Please reply to the list.
>>
>> Sure, your function can return an array if it's recursive. It returns
>> it to itself and uses whatever logic you need to add to the current
>> array or merge or remove or whatever. Regardless, I explained why you
>> aren't seeing the vars as global and a better way to do it using the
>> $GLOBALS array. I may be off but we would need to see the
>> applyForumChildrenTree() to know.
>>
>> -Shawn
>>
>>

> Yep I'm using globals, thanks .
>
> function getForumChildrenTree( $id )
> {
> $id = intval( $id );
> $GLOBALS['treeResult'] = array(
> 'topics' => array(),
> 'posts' => array(),
> 'forums' => array(),
> );
> applyForumChildrenTree( $id );
> $treeResult = $GLOBALS['treeResult'];
> unset($GLOBALS['treeResult']);
> return $treeResult;
> }

Good job.

Actually, if you used $GLOBALS['treeResult'] in applyForumChildrenTree()
then you could skip the:

$treeResult = $GLOBALS['treeResult'];
unset($GLOBALS['treeResult']);
return $treeResult;

and just:

return $GLOBALS['treeResult'];

or use $GLOBALS['treeResult']; somewhere later instead of returning it.

Also, I would be curious to see applyForumChildrenTree() because chances
are great that you can return an array and not use globals at all unless
it is "really" needed somewhere else in the app.

-Shawn
  Réponse avec citation
Vieux 12/03/2008, 04h57   #7
Lamonte
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Re: Setting a variable inside a function and making itglobalinsideaninner function doesn't work?

Shawn McKenzie wrote:
> Lamonte wrote:
>
>> Shawn McKenzie wrote:
>>
>>> Lamonte wrote:
>>>
>>>
>>>> Shawn McKenzie wrote:
>>>>
>>>>
>>>>> Shawn McKenzie wrote:
>>>>>
>>>>>
>>>>>
>>>>>> Lamonte wrote:
>>>>>>
>>>>>>
>>>>>>> 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 );
>>>>>>> }
>>>>>>>
>>>>>>>
>>>>>>> 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:
>>>>>>>
>>>>>>>
Citation:
>>>>>>> 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:
>>>>>>>
>>>>>>>
Code:
>>>>>>> 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:
>>>>>>>
>>>>>>>
Citation:
>>>>>>> Array
>>>>>>> (
>>>>>>> [topics] => Array
>>>>>>> (
>>>>>>> )
>>>>>>>
>>>>>>> [posts] => Array
>>>>>>> (
>>>>>>> )
>>>>>>>
>>>>>>> [forums] => Array
>>>>>>> (
>>>>>>> )
>>>>>>>
>>>>>>> )
>>>>>>>
>>>>>>>
>>>>>>> Is this a bug? I'm using PHP 5.2.4
>>>>>>>
>>>>>>>
>>>>>> Well you haven't defined anything global in your code, unless you did
>>>>>> inapplyForumChildrenTree(), in which case it needs to be global
>>>>>> everywhere that you want to use it as global.
>>>>>>
>>>>>> Easiest way would just be to use it like this everywhere:
>>>>>>
>>>>>> $GLOBALS['treeResult'] = array(
>>>>>> 'topics' => array(),
>>>>>> 'posts' => array(),
>>>>>> 'forums' => array(),
>>>>>> );
>>>>>>
>>>>>> -Shawn
>>>>>>
>>>>>>
>>>>> After a second look, probably a better alternate would be to have
>>>>> applyForumChildrenTree() return the array, then use $treeResult =
>>>>> applyForumChildrenTree( $id );
>>>>>
>>>>> -Shawn
>>>>>
>>>>>
>>>>>
>>>> As I said in my first reply, its a "Recursive function".so I couldn't
>>>> return the array.
>>>>
>>>>
>>>>
>>> Please reply to the list.
>>>
>>> Sure, your function can return an array if it's recursive. It returns
>>> it to itself and uses whatever logic you need to add to the current
>>> array or merge or remove or whatever. Regardless, I explained why you
>>> aren't seeing the vars as global and a better way to do it using the
>>> $GLOBALS array. I may be off but we would need to see the
>>> applyForumChildrenTree() to know.
>>>
>>> -Shawn
>>>
>>>
>>>

>> Yep I'm using globals, thanks .
>>
>> function getForumChildrenTree( $id )
>> {
>> $id = intval( $id );
>> $GLOBALS['treeResult'] = array(
>> 'topics' => array(),
>> 'posts' => array(),
>> 'forums' => array(),
>> );
>> applyForumChildrenTree( $id );
>> $treeResult = $GLOBALS['treeResult'];
>> unset($GLOBALS['treeResult']);
>> return $treeResult;
>> }
>>

> Good job.
>
> Actually, if you used $GLOBALS['treeResult'] in applyForumChildrenTree()
> then you could skip the:
>
> $treeResult = $GLOBALS['treeResult'];
> unset($GLOBALS['treeResult']);
> return $treeResult;
>
> and just:
>
> return $GLOBALS['treeResult'];
>
> or use $GLOBALS['treeResult']; somewhere later instead of returning it.
>
> Also, I would be curious to see applyForumChildrenTree() because chances
> are great that you can return an array and not use globals at all unless
> it is "really" needed somewhere else in the app.
>
> -Shawn
>
>

True, I could, actually I was trying something and my code is exactly
like that.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 17h32.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,27399 seconds with 15 queries