get_defined_vars doesn't return superglobals when...

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

get_defined_vars doesn't return superglobals when...

Post 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?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Cool to know.. I'll remember that one.. although I'm not sure I'd ever use it (foot in mouth).
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post 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:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The superglobals are defined in the global scope. Functions enjoy a different scope.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

my post doesn't answer that question? :?
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

don't panic Ninja, it does :)
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

being available and being defined are two entirely different things.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

What version of PHP are you using? That sounds like either a bug or a quirk.
(#10850)
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

feyd wrote:being available and being defined are two entirely different things.
That's what she said.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post 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.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
Post Reply