|
Posted by Chung Leong on 05/18/06 18:52
ojorus wrote:
> Hi!
> Just two short questions..:
>
> 1) In a function I want to get the value of a global variable declared
> outside the function. Should I use "global $variable" or $GLOBALS[variable]?
> Are there any differences in performance or other aspects which should make
> me choose the one thing or the other?
Access through $GLOBALS is slightly slower, since it requires two
look-ups: one for the $GLOBALS array, another for the actual element
within the array. Using the global keyword is the preferred method.
> 2) Generally, is it better to pass arguments to the function instead?
Yes, it's in general better to pass arguments. It makes your code more
flexible obviously as you can pass in different arguments. Using global
variables also makes your functions non-reentrant, which is serious
headache in any complex application.
Navigation:
[Reply to this message]
|