The use and difference between global static and $GLOBALS in php_PHP tutorial

WBOY
Release: 2016-07-13 17:14:43
Original
980 people have browsed it

This article summarizes and introduces the use and differences of global static and $GLOBALS in PHP. Friends who need to understand the differences between them can refer to it.

1.global works on the entire page.

Example 1

The code is as follows Copy code
 代码如下 复制代码

global $var1,$var2;

global $var1,$var2;

is a reference to an external variable with the same name, and the scope of the variable itself is still within the function body. Changing the values ​​of these variables will naturally change the external variables with the same name. But once & is used, the variable will no longer be a reference with the same name.
 代码如下 复制代码

$var1 = 1;$var2 = 2;
function test(){
global $var1,$var2;
// 作用范围在函数体内
$var1 = 3;}
test();
echo $var1;
?>

The code is as follows Copy code

$var1 = 1;$var2 = 2;

function test(){

global $var1,$var2;

// Scope is within the function body

$var1 = 3;}
代码如下 复制代码

$glpbal $a; $a=123; function f() { echo $a; //错误, }

test();

echo $var1;

?>

 代码如下 复制代码

function f()  {  global $a;  $a=123;  }   f();  echo $a; //正确,可以使用

The result is 3.

Because it is a reference with the same name

The PHP Global variables defined inside the function body can be used outside the function body. The global variables defined outside the function body cannot be used inside the function body.

The code is as follows Copy code
$glpbal $a; $a=123; function f() { echo $a; //Error, }
 代码如下 复制代码


/*
*author:ajax123
*qq:283400245
*/
class person{
static$name="ajax123";//static声明静态属性
static$age=25;//static声明静态属性
static$address="北京";//static声明静态属性
function song(){
echo "My name is : ".self::$name."
";//类内部:通过通过self 类访问静态属性
echo "I am ".self::$age."
";//类内部:通过通过self 类访问静态属性
echo "I live in ".self::$address."
";//类内部:通过self 类访问静态属性
}
}
echoperson::$name."
";//类外部:通过类名person访问静态属性
echoperson::$age."
";//类外部:通过类名person访问静态属性
echoperson::$address."
";//类外部:通过类名person访问静态属性
?>

Look at the following example
The code is as follows Copy code
function f() { global $a; $a=123; } f(); echo $a; //Correct, you can use
2.static only works within functions and classes. global and $GLOBALS are basically used the same, but are quite different in actual development. global generates an alias variable in the function that points to the external variable of the function, instead of the real external variable of the function. Once the address pointed to by the alias variable is changed, some unexpected situations will occur, Example 1.
The code is as follows Copy code
";//Internal class: access static properties through the self class echo "I am ".self::$age."
";//Inside the class: access static properties through the self class echo "I live in ".self::$address."
";//Inside the class: access static properties through the self class } } echoperson::$name."
";//External class: access static properties through class name person echoperson::$age."
";//External class: access static properties through class name person echoperson::$address."
";//External class: access static properties through class name person ?>

$GLOBALS[] is indeed called an external variable, and it will always be consistent inside and outside the function

Example #1 $GLOBALS Example

echo '$foo in global scope: ' . $GLOBALS["foo"] . "n";
The code is as follows
 代码如下 复制代码

function test() {
$foo = "local variable";

echo '$foo in global scope: ' . $GLOBALS["foo"] . "n";
echo '$foo in current scope: ' . $foo . "n";
}

$foo = "Example content";
test();
?>

Copy code

function test() {

$foo = "local variable";
echo '$foo in current scope: ' . $foo . "n";

}

$foo = "Example content"; ?> The output of the above routine is similar to: $foo in global scope: Example content $foo in current scope: local variable

http://www.bkjia.com/PHPjc/628908.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/628908.htmlTechArticleThis article summarizes and introduces the usage and differences of global static and $GLOBALS in php. Friends who need to understand the three differences between them Can be used for reference. 1.global works on the entire page. Example 1 The code is like...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template