Some questions about global and GLOBALS in php

Source: Internet
Author: User
For some global and GLOBALS problems in php, first take a simple example $ a = 'scpng'; functiontest () {global $ a; unset ($ a);} test (); var_dump ($ a); functiontest1 () {global and GLOBALS problems in un php

First, let's look at a simple example.

$a = 'scnjl';function test() {global $a;unset($a);}test();var_dump($a);function test1() {unset($GLOBALS['a']);}test1();var_dump($a);

? The output result is as follows:

String (5) "scnjl" NULL

We can see that when using the global function, the variable $ a is not directly used. Instead, a variable pointing to the value of $ a is copied. Therefore, after unset, the global variable $ a is not unset, and $ GLOBALS ['A'] directly represents the global variable $.

This is the difference between global and GLOBALS.

?

Today, when I got something, I found that the global variable has no value in the class. I found it for a long time because the value in the file included in that class is not a global variable, because the file is included by another function, the variables in the include file in the class file are also local variables.

Example:

Test008.php

include 'test009.php';Class A {var $name;function __construct() {}function A() {$this->__construct();}function test() {var_dump($GLOBALS['var']);}function test1() {$this->test();}}$a = new A();

? Test009.php

$var = 'scnjl';

? Test010.php

class xx {function __construct() {}function test() {include 'test008.php';$a->test1();}}$xx = new xx();$xx->test();

? This is just a simple column. In fact, the values in test009.php can be directly put into test008.php.

Here, a null value is output.

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.