Usage of php9 Super global variables (1)

Source: Internet
Author: User
Tags server array
Many predefined variables in PHP are & ldquo; Super Global & rdquo;, which means they are available in all scopes of a script. You can access functions or methods without performing global $ variable. These super global variables are: $ GLOBALS $ _ SERVER $ _ GET $ _ POST $

Many predefined variables in PHP are "super Global", which means they are available in all scopes of a script. You can access functions or methods without performing global $ variable.

These super global variables are:

  • $ GLOBALS
  • $ _ SERVER
  • $ _ GET
  • $ _ POST
  • $ _ FILES
  • $ _ COOKIE
  • $ _ SESSION
  • $ _ REQUEST
  • $ _ ENV1. let's take a look at $ GLOBALS, which is a global combination array containing all the variables. what does it mean? look at a C language program.
    int main(){      int a = 3;      void t()     {     printf("%d",a);     }     t();     return 0;}


    If the program runs, it must output a. is it easy to understand? output the variable in the t () function. But let's take a look at a php program:

       


    Will this output 3? No, that would be naive. it's impossible to lose everything, why ??? It's easy, because $ a is not a global variable, and its value cannot be exceeded in the t () function. wow ~ What if the world is dark ?? Don't be nervous. at this time, our $ GLOBALS is needed. I just mentioned that it is a global combination array containing all the variables, which may not be understood by everyone, now it should be clear, that is, through $ GLOBALS, you can get the value of $ a in the t () function, the method $ GLOBAL ['$ a'], try changing the t () function content to function t (){
    Echo $ GLOBALS ['A'];
    } Run it, and the value of $ a is displayed clearly on the page. Let's talk about the $ GLOBAL range. you can obtain the value in the require and include pages on your current page and the current page. It's amazing. Of course, it is not omnipotent. let's take a look at this
       

    What is output ??? The answer is only 5, that is, $ GLOBALS cannot get the values in other functions.

    Let's look at the second $ _ SERVER,$ _ SERVERIs an array containing information such as header, path, and script locations. The items in this array are created by the Web server. It is not guaranteed that each server provides all projects. the server may ignore some projects or provide some projects that are not listed here. You can extract a lot of useful information from $ _ SERVER. for example, $ _ SERVER ['remote _ ADDR '] can get the ip address of the current user, next, I use foreach to traverse the entire $ _ SERVER array and print it. The code is as follows:

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


    If you do not want to see the effect or cannot write it, you can view this url. this is the effect of the sae server. Http://5253.sinaapp.com/blog/server.phpi will upload the token 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.