global or $GLOBALS

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ojorus

    #1

    global or $GLOBALS

    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?

    2) Generally, is it better to pass arguments to the function instead?

    Ole J


  • Erwin Moller

    #2
    Re: global or $GLOBALS

    ojorus wrote:
    [color=blue]
    > Hi!
    > Just two short questions..:[/color]

    Hi,
    [color=blue]
    >
    > 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?[/color]

    Just use:
    global $varname;
    inside your function.

    Don't worry about performance issues for such basic stuff unless you have a
    valid reason.
    [color=blue]
    >
    > 2) Generally, is it better to pass arguments to the function instead?[/color]

    That depends.
    I think you mean if passing by reference or plain passing?

    Passing by reference is faster, because normal passing involves a complete
    copy of the variable.
    If you pass a variable by reference, the pointer to the object is passed.
    a possible handy or harmfull sideeffect of passing by reference is that the
    function can change the content of the passed variable.

    Read more here:


    Happy coding. :-)

    regards,
    Erwin Moller
    [color=blue]
    >
    > Ole J[/color]

    Comment

    • Chung Leong

      #3
      Re: global or $GLOBALS

      ojorus wrote:[color=blue]
      > 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?[/color]

      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.
      [color=blue]
      > 2) Generally, is it better to pass arguments to the function instead?[/color]

      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.

      Comment

      • Thomas Mlynarczyk

        #4
        Re: global or $GLOBALS

        Also sprach Erwin Moller:
        [color=blue]
        > Passing by reference is faster, because normal passing involves a
        > complete copy of the variable.[/color]

        No. See http://www.zend.com/zend/art/ref-count.php

        Quote: "Note that PHP 4 is not like C: passing variables by reference is not
        necessarily faster than passing them by value. Indeed, in PHP 4 it is
        usually better to pass a variable by value, except if the function changes
        the passed value or if a reference ... is being passed."

        PHP simply creates another entry in the symbol table pointing to the same
        stored value and does not copy it until one of the variables is actually
        changed. Thus, references would not necessarily be faster.

        Greetings,
        Thomas


        Comment

        Working...