www.webdeveloper.com
Results 1 to 10 of 10

Thread: global variables vs $_GET

  1. #1
    Join Date
    Jan 2008
    Posts
    273

    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

  2. #2
    Join Date
    Mar 2010
    Posts
    2,803
    Hopefully this explains what $_GET is and how it works.

  3. #3
    Join Date
    Jan 2008
    Posts
    273
    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

  4. #4
    Join Date
    Jun 2010
    Posts
    3
    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:
    "The built-in $_GET function is used to collect values in a form with method="get"."

  5. #5
    Join Date
    Jan 2008
    Posts
    273
    ok, thats the missing link, there are globals and superglobals!

    thnx

  6. #6
    Join Date
    Mar 2010
    Posts
    2,803
    $_GET is a superglobal

  7. #7
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,175
    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.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  8. #8
    Join Date
    Jan 2008
    Posts
    273
    Quote Originally Posted by NogDog View Post
    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.
    So the conclusion is: Don't use globals?!

  9. #9
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,175
    Quote Originally Posted by jeanluca View Post
    So the conclusion is: Don't use globals?!
    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.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  10. #10
    Join Date
    Jan 2008
    Posts
    273
    ok, thnx a lot for explaining!!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles