What is the difference between global and $ globals in PHP?

Source: Internet
Author: User
What is the difference between global and $ globals in PHP? Paste the code first. this is global.
 

After testglobal is called, assign a value to $ var1 and then print the values of $ var1 and $ var2.
The output result is
5---5 second output: 20---10
Assign the address of var1 to var2 in the function (I tested that changing the value of var1 in the function body will also change the value of var2), but assign a value to var2 after the call, the value of var1 is not changed. modifying the value of var1 after global will affect the value of var1 in vitro of the function.

This is $ globals code
 

Using $ globals changes the values of var1 and var2 both in the function body and in vitro.

Problem:
1: Does a global reference variable belong to a pass-through reference. after referencing a variable, it actually only transmits the value of the variable to the function body? However, changing the value of var1 in global affects the value of var1 in vitro. why?
2: What are the differences between global and $ globals?

I am looking forward to getting some advice. the format of the first question is a bit messy and I will improve it.


Reply to discussion (solution)

Don't worry about this problem? The manual already has a clear description


Global $ var1, $ var2; // $ var1 and $ var2 are references to global variables.
$ Var2 = & $ var1; // assign $ var2 a value to $ var1. This removes $ var2's reference to the global variable $ var2.

Don't worry about this problem? The manual already has a clear description


Global $ var1, $ var2; // $ var1 and $ var2 are references to global variables.
$ Var2 = & $ var1; // assign $ var2 a value to $ var1. This removes $ var2's reference to the global variable $ var2.


Thank you. please take a look at the next Manual.

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.