Page 1 of 3

get_defined_vars doesn't return superglobals when...

Posted: Tue Aug 15, 2006 9:39 am
by Oren
get_defined_vars() doesn't return superglobals when used inside a function. Not that I have a problem with this... It actually exactly what I needed, but I'm still wondering why this:

Code: Select all

<?php

 	require_once 'file.php';

	$arr = get_defined_vars();

	print_r($arr);
and this:

Code: Select all

<?php

 	function test()
 	{
 	 	require_once 'file.php';

	 	$arr = get_defined_vars();

	 	print_r($arr);
 	}

 	test();
don't return the same values. The latter, somehow, doesn't return the superglobals variables (e.g $_GET, $_POST, $_COOKIE).

Any ideas?

Posted: Tue Aug 15, 2006 9:40 am
by Luke
straight out the manual, yo!
The Manual wrote:Description
array get_defined_vars ( void )

This function returns a multidimensional array containing a list of all defined variables, be them environment, server or user-defined variables, within the scope that get_defined_vars() is called.

Posted: Tue Aug 15, 2006 10:20 am
by Benjamin
Cool to know.. I'll remember that one.. although I'm not sure I'd ever use it (foot in mouth).

Posted: Tue Aug 15, 2006 10:34 am
by Oren
The Ninja Space Goat wrote:straight out the manual, yo!
The Manual wrote:Description
array get_defined_vars ( void )

This function returns a multidimensional array containing a list of all defined variables, be them environment, server or user-defined variables, within the scope that get_defined_vars() is called.
Read my post again dude :wink:

Posted: Tue Aug 15, 2006 10:38 am
by feyd
The superglobals are defined in the global scope. Functions enjoy a different scope.

Posted: Tue Aug 15, 2006 10:39 am
by Luke
my post doesn't answer that question? :?

Posted: Tue Aug 15, 2006 10:46 am
by Jenk
don't panic Ninja, it does :)

Posted: Tue Aug 15, 2006 10:51 am
by Oren
The Ninja Space Goat wrote:my post doesn't answer that question? :?
I'm afraid it doesn't.
feyd wrote:The superglobals are defined in the global scope. Functions enjoy a different scope.
They are superglobals... From the manual (referring to superglobals):
they are automatically global--i.e., automatically available in every scope.

Posted: Tue Aug 15, 2006 10:57 am
by feyd
being available and being defined are two entirely different things.

Posted: Tue Aug 15, 2006 11:09 am
by Christopher
What version of PHP are you using? That sounds like either a bug or a quirk.

Posted: Tue Aug 15, 2006 11:57 am
by Benjamin
feyd wrote:being available and being defined are two entirely different things.
That's what she said.

Posted: Tue Aug 15, 2006 2:12 pm
by Oren
feyd wrote:being available and being defined are two entirely different things.
I know that, but how is this related? The superglobals are defined in the script and available (outside and inside) the function.

Posted: Tue Aug 15, 2006 2:13 pm
by Jenk
You've just said it yourself.. They are defined outside of the function on the global scope.. defined.. outside of the function.. not inside.. etc.

Posted: Tue Aug 15, 2006 2:27 pm
by Oren
You didn't understand me. Ok, I'll make it much more simple. When running this code:

test.php?var=foo:

Code: Select all

<?php

        function test()
        {
               require_once 'file.php';

                $arr = get_defined_vars();

                print_r($arr);

                echo $_GET['var'];
        }

        test();
"foo" is printed. Meaning it is defined and available within the function too.

Posted: Tue Aug 15, 2006 3:15 pm
by feyd
You're having a real hard time understanding the differences between defined and available, aren't you? PHP is doing magic behind the scenes to make the request for $_GET['var'] to work. That's why they're called superglobals. They're defined once, available everywhere.