Difference between global and $ GLOBALS [] in PHP

Source: Internet
Author: User
In PHP, global and $ GLOBALS [] are different. many people think that the difference between global and $ GLOBALS [] is just the difference in writing the above code. According to the official explanation, 1. $ GLOBALS [var] is the external global variable itself. 2. global $ var is the reference or pointer of the external $ var with the same name. For example: <? Php ?? $ Var1 ?? 1 ;?? Difference between global and $ GLOBALS [] in $ PHP

Many people think that the difference between global and $ GLOBALS [] is actually not the same.

According to the official explanation

1. $ GLOBALS ['var'] is an external global variable.

2. global $ var is an external reference or pointer with the same name as $ var.

For example:

  1. <? Php ??
  2. $ Var1? =? 1 ;??
  3. $ Var2? =? 2 ;??
  4. Function? Test (){??
  5. ???? $ GLOBALS ['var2']? =? & $ GLOBALS ['var1'];?
  6. }??
  7. Test ();??
  8. Echo? $ Var2 ;??
  9. ?> ??

The normal print result is 1.

  1. <? Php ??
  2. $ Var1? =? 1 ;??
  3. $ Var2? =? 2 ;??
  4. Function? Test (){??
  5. ???? Global? $ Var1, $ var2 ;??
  6. ???? $ Var2? =? & $ Var1 ;??
  7. }??
  8. Test ();??
  9. Echo? $ Var2 ;??
  10. ?> ??

The unexpected result is 2.

Why is the result 2 printed? In fact, it is because the $ var1 reference points to the reference address of $ var2. The actual value is not changed.

Let's look at an example.

  1. <? Php ??
  2. $ Var1? =? 1 ;??
  3. Function? Test (){??
  4. ???? Unset ($ GLOBALS ['var1']);?
  5. }??
  6. Test ();??
  7. Echo? $ Var1 ;??
  8. ?> ??

Because $ var1 is deleted, nothing is printed.

  1. <? Php ??
  2. $ Var1? =? 1 ;??
  3. Function? Test (){??
  4. ???? Global ?? $ Var1 ;??
  5. ???? Unset ($ var1 );??
  6. }??
  7. Test ();??
  8. Echo? $ Var1 ;??
  9. ?> ??

Accidentally printed 1. It indicates that only the alias is deleted. | the referenced value is not changed.

Do you understand?

That is to say, global $ var is actually $ var = & $ GLOBALS ['var']. Call an alias for an external variable.

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.