Does PHP support global variables? How to solve it

Source: Internet
Author: User
Does PHP support global variables?
Can you define a global variable, give it a different value in a different page, and then call it.

Like defining var=0.
In the a.php
Var=1;
Include c.php
In the b.php
var=2;
Include c.php
Judging in the c.php
if (var==1)
Include c1.php
if (var==2)
Include c2.php
Else
...

Try to write this, it seems not to.
It is possible to write this variable to the session.

------Solution--------------------
No, PHP does not have a global variable and can be saved with a database or a file to read
------Solution--------------------
There are global variables
For example
$var = 0; written in config.php

c.php in Include_once (' config.php ');
Then you can use the $var variable

But if you want to use $var in a function
Add global $var;
------Solution--------------------
discuss

There are global variables
For example
$var = 0; written in config.php

c.php in Include_once (' config.php ');
Then you can use the $var variable

But if you want to use $var in a function
Add global $var;

------Solution--------------------
Many kinds of. The session is one of them.
------Solution--------------------
Word games?
Global's
Variables that are outside of functions and classes are global variables

To leave a separate PHP program, PHP is called a system variable. System variable is read-only

$_get, $_post, $_session, etc. are super global variables

------Solution--------------------
The session in PHP can be set to the global variable, the value will be stored in it, valid until you delete it (the cookie also has a similar practice)
------Solution--------------------
It is defined in a common file, and then it is included in all files, even if it is a global variable ...

Session, database, hidden field values these can also be used as global variables.
------Solution--------------------
Of course support
------Solution--------------------
Put the variable into the object inside, like PHP singleton mode, can not it? We can change the values, or we can call them globally.
------Solution--------------------
Explore

Word games?
Global's
Variables that are outside of functions and classes are global variables

To leave a separate PHP program, PHP is called a system variable. System variable is read-only

$_get, $_post, $_session, etc. are super global variables
  • 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.