/    Sign up×
Community /Pin to ProfileBookmark

global variables vs $_GET

Hi All

I noticed that variables like $_GET are available everywhere in the program. They are different from global variables (these need to be defined ‘global’ in a function)

What exactly is the difference between them and can i define variables like $_GET, so I don’t have to define them in each function ?

cheers

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@tirnaJun 17.2010 — Hopefully this explains what $_GET is and how it works.
Copy linkTweet thisAlerts:
@jeanlucaauthorJun 17.2010 — it does explain how to use it, but doesn't mention anything about global variables.

I want to know more about this variable compaired with global variables; you have to agree global variables and (for example) $_GET look like globals, but $_GET is different
Copy linkTweet thisAlerts:
@edgaralvesJun 17.2010 — If you want to use global variables without using the "global", u can use the superglobal $GLOBALS['var_name'].

For example:

$var = "test";

function print_test() {

echo $GLOBALS['var'];

}


As the link tirna posted says:

"[I]The built-in $_GET function is used to collect values in a form with method="get".[/I]"
Copy linkTweet thisAlerts:
@jeanlucaauthorJun 17.2010 — ok, thats the missing link, there are globals and superglobals!

thnx
Copy linkTweet thisAlerts:
@tirnaJun 17.2010 — $_GET is a superglobal
Copy linkTweet thisAlerts:
@NogDogJun 17.2010 — One caveat: if you make a function dependent upon $GLOBAL, it is effectively the same as using a "global" variable declaration within the function: it is now closely coupled with the application code and thus inherits all the same problems that cause most developers to eschew the use of "global" (re-usability issues, testing issues, potential for nasty bugs issues, etc.)

The "standard" super-global arrays such as $_POST and $_GET aren't quite as bad, as they are, well, standard. But even then using them directly within a function may be less than desirable, as opposed to passing the necessary variables in the function parameters, whether by copy or by reference.
Copy linkTweet thisAlerts:
@jeanlucaauthorJun 17.2010 — One caveat: if you make a function dependent upon $GLOBAL, it is effectively the same as using a "global" variable declaration within the function: it is now closely coupled with the application code and thus inherits all the same problems that cause most developers to eschew the use of "global" (re-usability issues, testing issues, potential for nasty bugs issues, etc.)

The "standard" super-global arrays such as $_POST and $_GET aren't quite as bad, as they are, well, standard. But even then using them directly within a function may be less than desirable, as opposed to passing the necessary variables in the function parameters, whether by copy or by reference.[/QUOTE]


So the conclusion is: Don't use globals?!
Copy linkTweet thisAlerts:
@NogDogJun 17.2010 — So the conclusion is: Don't use globals?![/QUOTE]

I won't say never, ever use them; but generally speaking it's almost always better not to in terms of re-usability and all that other stuff. About the only time I'd say not to worry about it is if you are writing a quick, one-off script that you never expect to re-use in any way or have to maintain. ?
Copy linkTweet thisAlerts:
@jeanlucaauthorJun 17.2010 — ok, thnx a lot for explaining!!
×

Success!

Help @jeanluca spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.13,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...