Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / PHP / General PHP Topics / July 2009



Tip: Looking for answers? Try searching our database.

Why $GLOBALS ?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
j.keßler - 29 Jul 2009 12:55 GMT
Hello everyone,

well I use PHP for a long time and currently I have one question:

Why is there this $GLOBALS varianble?
http://www.php.net/manual/en/reserved.variables.globals.php

It is always there, but why ?
Is it a good thing to use this or not ?

Any comments about this will be nice.

regards,

j. keßler
Álvaro G. Vicario - 29 Jul 2009 13:09 GMT
j.keßler escribió:
> well I use PHP for a long time and currently I have one question:
>
> Why is there this $GLOBALS varianble?
> http://www.php.net/manual/en/reserved.variables.globals.php
>
> It is always there, but why ?

It's a superglobal (like $_GET, $_POST or $_SESSION) and superglobals
are always available by design, even though if they're empty.
(Apparently, its name doesn't start with underscore because it's way
older than the rest.)

> Is it a good thing to use this or not ?

Are you referring to global variables or to this specific syntax?

I believe it's generally accepted as good practice to avoid global
variables as much as possible. Apart from performance questions, it's
harder to debug code when there're variables popping in from nowhere, or
being used God knows where else.

The syntax can help to identify each use of the variable. If you use the
alternative, global $foo, you can easily overlook the fact you are using
a global variable.

Signature

-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--

j.keßler - 29 Jul 2009 14:22 GMT
> j.keßler escribió:
>> well I use PHP for a long time and currently I have one question:
[quoted text clipped - 8 lines]
> (Apparently, its name doesn't start with underscore because it's way
> older than the rest.)
Yea but why is this needed ?
$_GET has the GET Data.
$_POST has the POST data
(..)

But you could also get the GET data with $GLOBALS['GET'] ...

>> Is it a good thing to use this or not ?
>
> Are you referring to global variables or to this specific syntax?

no.
I mean this:

$var = 1;
echo $var: # prints 1
echo $GLOBALS['var']; # prints also  1

so the global could be useless since you can access the varible you need via
$GLOBALS

> I believe it's generally accepted as good practice to avoid global
> variables as much as possible. Apart from performance questions, it's
[quoted text clipped - 4 lines]
> alternative, global $foo, you can easily overlook the fact you are using
> a global variable.
Erwin Moller - 29 Jul 2009 14:39 GMT
j.keßler schreef:

>> j.keßler escribió:
>>> well I use PHP for a long time and currently I have one question:
[quoted text clipped - 26 lines]
> so the global could be useless since you can access the varible you need via
> $GLOBALS

Hi,

That happens because you create it in global scope.
If you use a variable inside a function as shown below, it won't be
stored in $GLOBALS.

<?php

// $var created in global scope:
$var = 1;
echo $GLOBALS['var'];

doSomething();

// will result in an error
echo $GLOBALS['anothervar'];

function doSomething(){
    $anothervar = 10;   
}

?>

But that aside: I never use $GLOBALS, only $_GET, $_POST, and $_COOKIE.
IMHO: $GLOBALS is just a dinosaur, but it didn't really die yet.
If you have no need for it, don't use it: just remember it exists. ;-)

Regards,
Erwin Moller

Signature

"There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult."
-- C.A.R. Hoare

sheldonlg - 29 Jul 2009 18:23 GMT
> j.keßler schreef:
>
[quoted text clipped - 59 lines]
> Regards,
> Erwin Moller

However, there is also $_SESSION, a global, which is quite useful.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2010 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.