Question about PHP global variables

Source: Internet
Author: User
Ask the PHP global variable question? & Nbsp; I declare a global variable $ GLOBALS [& quot; x & quot;] = 1; & nbsp; in 1. in php, redefine $ GLOBALS [& quot; x & quot;] = 2; & nbsp; in 2. in php, can I ask about PHP global variables?

I declare a global variable $ GLOBALS ["x"] = 1;
Redefine $ GLOBALS ["x"] = 2 in 1. php;
In 2.php, the result of $ GLOBALS ["x"] is still 1;

My idea is that there is a global variable. if it is modified, its value will be subject to the newly modified value. I don't know how to deal with it in PHP?

------ Solution --------------------
1. php
Session_start ();
$ _ SESSION ['x'] = 1;

2. php
Session_start ();
$ _ SESSION ['x'] = 2;

3. php
Session_start ();
Echo $ _ SESSION ['x'];




------ Solution --------------------
You use
Global $ x = 1;
Global $ x = 2;
Echo $ x;
Try

Unlike $ GLOBALS ['x '];
------ Solution --------------------
$ GLOBALS ["x"] = 1; // temporary global variable. It is not a global variable in a session. So B. php shows 1
------ Solution --------------------
Global variables are only valid in the same PHP file. sessions are required for different php files.
------ Solution --------------------
Only sessions can access each other on each page!

The variables defined or accessed by gloabl and $ GLOBALS are all variables on this page!

To access variables in other files, you need to load the file!

There is a fundamental difference with session!

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.