Scratching my head over this one.

I see very little difference between $GLOBALS and get_defined_vars(), but when I use get_defined_vars() inside a function it only seems to return variables within the local scope of the function!

(this is latest PHP 4)

What the heck...?

    get_defined_vars returns all the variables within the local scope, yes...

    But what's that got to do with $GLOBALS?

    Cheers

      PHP Manual entry for get_defined_vars():
      "This function returns an multidimensional array containing a list of all defined variables, be them environment, server or user-defined variables"

      It doesn't say anything about scope. Where might I have found that aspect documented? Or did you just figure that out yourself via trial&error?

      The reason I refer to GLOBALS is that, as far as I can see, it has the same value as get_defined_vars() (when get_defined_vars() is called outside a function, that is).

      But thanks for the confirmation, colinhorne!

      BTW, array_keys(array_slice($GLOBALS,13)) seems to return the names of user-defined variables. Don't know whether that's always (using 13 as an index) the case...
      But I like it...

        It's not exactly documentation, I guess, but the latest (topmost) user comment in the manual page for get_defined_vars() asks and answers the same question.

          It's not exactly documentation, I guess, but the latest (topmost) user comment in the manual page for get_defined_vars() asks and answers the same question.

          Well, I'll be...!
          So it does...!
          I have a manual that I downloaded and installed locally, but despite being by the same authors, it lacks the user comments.
          I didn't realize there were different manual versions, so I should get on top of that!

          Thanks, "Installer" (guess I'd better live up to your name)!

          (seems the behavior is pretty much as I described)

            Heh, yeah. I checked the manual before I posted my first comment. I thought it was you who posted the comment on the man...

            (Yeah, I just did trail + error to find out)

            Cheers!

              Write a Reply...