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
Oren
DevNet Resident
Posts: 1640 Joined: Fri Apr 07, 2006 5:13 am
Location: Israel
Post
by Oren » Tue Aug 15, 2006 9:39 am
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?
Luke
The Ninja Space Mod
Posts: 6424 Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA
Post
by Luke » Tue Aug 15, 2006 9:40 am
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.
Benjamin
Site Administrator
Posts: 6935 Joined: Sun May 19, 2002 10:24 pm
Post
by Benjamin » Tue Aug 15, 2006 10:20 am
Cool to know.. I'll remember that one.. although I'm not sure I'd ever use it (foot in mouth).
Oren
DevNet Resident
Posts: 1640 Joined: Fri Apr 07, 2006 5:13 am
Location: Israel
Post
by Oren » Tue Aug 15, 2006 10:34 am
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
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Tue Aug 15, 2006 10:38 am
The superglobals are defined in the global scope. Functions enjoy a different scope.
Luke
The Ninja Space Mod
Posts: 6424 Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA
Post
by Luke » Tue Aug 15, 2006 10:39 am
my post doesn't answer that question?
Jenk
DevNet Master
Posts: 3587 Joined: Mon Sep 19, 2005 6:24 am
Location: London
Post
by Jenk » Tue Aug 15, 2006 10:46 am
don't panic Ninja, it does
Oren
DevNet Resident
Posts: 1640 Joined: Fri Apr 07, 2006 5:13 am
Location: Israel
Post
by Oren » Tue Aug 15, 2006 10:51 am
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.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Tue Aug 15, 2006 10:57 am
being available and being defined are two entirely different things.
Christopher
Site Administrator
Posts: 13596 Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US
Post
by Christopher » Tue Aug 15, 2006 11:09 am
What version of PHP are you using? That sounds like either a bug or a quirk.
(#10850)
Benjamin
Site Administrator
Posts: 6935 Joined: Sun May 19, 2002 10:24 pm
Post
by Benjamin » Tue Aug 15, 2006 11:57 am
feyd wrote: being available and being defined are two entirely different things.
That's what she said.
Oren
DevNet Resident
Posts: 1640 Joined: Fri Apr 07, 2006 5:13 am
Location: Israel
Post
by Oren » Tue Aug 15, 2006 2:12 pm
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.
Jenk
DevNet Master
Posts: 3587 Joined: Mon Sep 19, 2005 6:24 am
Location: London
Post
by Jenk » Tue Aug 15, 2006 2:13 pm
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.
Oren
DevNet Resident
Posts: 1640 Joined: Fri Apr 07, 2006 5:13 am
Location: Israel
Post
by Oren » Tue Aug 15, 2006 2:27 pm
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.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Tue Aug 15, 2006 3:15 pm
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.