Php 简明教程
PHP - Global Variables
在 PHP 中,任何可以在 PHP 脚本中的任何地方访问的变量都称为 global variable 。如果变量在脚本中的所有函数或类之外声明,它将成为全局变量。
In PHP, any variable that can be accessed from anywhere in a PHP script is called as a global variable. If the variable is declared outside all the functions or classes in the script, it becomes a global variable.
虽然全局变量可以在函数外部直接访问,但它们不会自动在函数内部提供。
While global variables can be accessed directly outside a function, they aren’t automatically available inside a function.
Example
在下面的脚本中, $name 对于函数 sayhello() 是全局的。
In the script below, $name is global for the function sayhello().
<?php
$name = "Amar";
function sayhello() {
echo "Hello " . $name;
}
sayhello();
?>
但是,该变量在该函数内部不可访问。因此,您将收到 error message "未定义变量 $name"。
However, the variable is not accessible inside the function. Hence, you will get an error message "Undefined variable $name".
Hello
PHP Warning: Undefined variable $name in /home/cg/root/93427/main.php on line 5
Example
要在函数内访问,您需要在变量之前使用 "global" 关键字。
To get access within a function, you need to use the "global" keyword before the variable.
<?php
$name = "Amar";
function sayhello() {
GLOBAL $name;
echo "Hello " . $name;
}
sayhello();
?>
它将生成以下 output −
It will produce the following output −
Hello Amar
如果某个函数访问全局变量并修改该变量,则修改后的值在函数调用完成后随处可用。
If a function accesses a global variable and modifies it, the modified value is available everywhere after the function call is completed.
让我们在 sayhello() 函数中更改 $name 的值,并在调用该函数后检查其值。
Let us change the value of $name inside the sayhello() function and check its value after the function is called.
Example
请查看以下示例:
Take a look at this following example −
<?php
$name = "Amar";
function sayhello() {
GLOBAL $name;
echo "Global variable name: $name" .PHP_EOL;
$name = "Amarjyot";
echo "Global variable name changed to: $name" .PHP_EOL;
}
sayhello();
echo "Global variable name after function call: $name" .PHP_EOL;
?>
它将生成以下 output −
It will produce the following output −
Global variable name: Amar
Global variable name changed to: Amarjyot
Global variable name after function call: Amarjyot
The $GLOBALS Array
PHP 维护一个名为 $GLOBALS 的关联数组,其中包含在全局作用域中声明的所有变量及其值。$GLOBALS 数组还存储许多称为超全局变量的预定义变量以及用户定义的全局变量。
PHP maintains an associative array named $GLOBALS that holds all the variables and their values declared in a global scope. The $GLOBALS array also stores many predefined variables called as superglobals, along with the user defined global variables.
任何全局变量也可以在任何函数内访问,方法是在访问箭头元素的常规语法中使用它。例如,全局变量 $name 的值由 $GLOBALS["name"] 提供。
Any of the global variables can also be accessed inside any function with the help of a regular syntax of accessing an arrow element. For example, the value of the global variable $name is given by $GLOBALS["name"].
Example
在下面的示例中,在 addition() 函数内部访问了两个全局变量 $x 和 $y。
In the following example, two global variable $x and $y are accessed inside the addition() function.
<?php
$x = 10;
$y = 20;
function addition() {
$z = $GLOBALS['x']+$GLOBALS['y'];
echo "Addition: $z" .PHP_EOL;
}
addition();
?>
它将生成以下 output −
It will produce the following output −
Addition: 30
Example
您还可以通过将它添加到 $GLOBALS 数组中将任何局部变量添加到全局作用域。让我们在全局作用域中添加 $z 。
You can also add any local variable into the global scope by adding it in the $GLOBALS array. Let us add $z in the global scope.
<?php
$x = 10;
$y = 20;
function addition() {
$z = $GLOBALS['x']+$GLOBALS['y'];
$GLOBALS['z'] = $z;
}
addition();
echo "Now z is the global variable. Addition: $z" .PHP_EOL;
?>
它将生成以下 output −
It will produce the following output −
Now z is the global variable. Addition: 30
Including One PHP Script in Another
您可以在另一个 PHP 脚本中包含一个 PHP 脚本。包含在脚本中的变量被添加到包含它的 PHP 脚本的全局作用域中。
You can include one PHP script in another. Variables declared in the included script are added in the global scope of the PHP script in which it is included.
这是 "a.php" 文件 -
Here is "a.php" file −
<?php
include 'b.php';
function addition() {
$z = $GLOBALS['x']+$GLOBALS['y'];
echo "Addition: $z" .PHP_EOL;
}
addition();
?>
其中包括有 $x 和 $y 变量的 “b.php”,因此它们变成 “a.php” 脚本中 addition() 函数的全局变量。
It includes "b.php" that has the $x and $y variables, so they become the global variables for the addition() function in "a.php" script.
<?php
$x = 10;
$y = 20;
?>
在实现singleton模式、获取嵌入式系统寄存器以及一个变量被多个函数使用时,通常会使用全局变量。
Global variables are generally used while implementing singleton patterns, and accessing registers in embedded systems and also when a variable is being used by many functions.