PHP9 of the use of a super global variable (i)

Source: Internet
Author: User
Tags foreach array header key variables variable

Many of the predefined variables in PHP are "hyper-global", which means they are available in all scopes of a script. The global $variable is not required in a function or method; You can access them.

These hyper-global variables are:

  • $GLOBALS
  • $_SERVER
  • $_GET
  • $_POST
  • $_FILES
  • $_COOKIE
  • $_SESSION
  • $_REQUEST
  • $_ENV1. Take a look at the $globals, which is a global array containing all the variables, what does it mean to see a C language program
    int main ()
    {
          int a = 3;
          void T ()
         {
         printf ("%d", a);
         }
         T ();
         return 0;
    }


    This program is definitely output a, it is easy to understand, in the T () function to output a variable. But look at one of the PHP programs:

     
        


    Is this going to output 3? No,no,no that is too naïve, it is not the output of anything, why??? Very simple, because $a is not a global variable, the T () function inside the region does not have its value, wow ~ world dark, how to do?? Don't be nervous, this time we need our $globals to play, just as I said it is a global array containing all the variables may not understand, now said should be more clear, that is, through the $globals can be in the T () function to take the $a value, method $global[' $ A '], try changing the T () function content to function t () {
    echo $GLOBALS [' a '];
    Run it, the page will clearly show the value of $a. Again, the range of $global is available on both your current page and the current page require and include the values in the page. It's not bad. Of course, it's not everything, and look at this.
     
        

    What does it output??? The answer is only 5, which means that $globals cannot be taken to the values in other functions.

    Look at the second $_server, which $_SERVER contains an array of information such as header information (header), path (path), and script location (scripts locations), and so on. The items in this array are created by the WEB server. There is no guarantee that every server will provide all the items, the server may ignore some, or provide some items that are not listed here. From the $_server can extract a lot of useful information, such as $_server[' REMOTE_ADDR ' can get the current user's IP, I use foreach traverse the entire $_server array, print it. The code is as follows:

     
        $value) {
    echo$key: $value
    \ n "; }


    Want to see the effect do not want or can't write now can look at this web site, this is the SAE server servers effect. Http://5253.sinaapp.com/blog/server.php I uploaded it here.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.