变量是任何编程语言的重要组成部分。您可以使用它们来存储各种信息,如整数、浮点数、字符串、数组、文件的内容等。然后可以通过添加或删除信息来操作存储在变量中的数据。使用变量还允许我们创建循环并重复执行一些任务。您还可以定义将不同变量作为参数并返回新数据的函数。
在本教程中,我们将了解一个称为变量作用域的重要概念以及它如何影响您在 php 中编写代码的方式。我们还将介绍globalandstatic关键字的用法。
什么是变量范围?
变量用于以后存储和访问或操作信息,但您不能只从您喜欢的任何地方访问它们的数据。根据您使用的编程语言,某些变量可能在某些地方变得不可访问。
简单来说,变量的范围决定了它在程序不同部分的可用性。
PHP中的变量范围
PHP 对变量作用域有一种易于理解和开始友好的方法。
基本上,您在 PHP 文件某处定义的变量在定义后几乎在任何地方都可见或可用。您还可以在使用include()和添加到程序的其他文件中使用它require()。
假设有一个名为variables.php的文件,它包含以下代码:
<?php$apple_count = 12;?>
该变量$apple_count也可以在另一个文件中使用,代码如下:
<?php include('variables.php'); echo 'There are '.$apple_count.' apples in the basket.'; // There are 12 apples in the basket. ?>
但是,此变量在用户定义的函数中不可用。这是一个例子:
<?php include('variables.php'); echo 'There are '.$apple_count.' apples in the basket.'; function fruit_count() { echo 'There are still '.$apple_count.' apples in the basket.'; } fruit_count(); // // Notice about Undefined Variable ?>
因此,在用户定义函数之外定义的变量在函数中不可用。同样,在函数中定义的变量也存在于该函数中。这就是为什么当我们尝试在函数$mango_count外部回显的值时会收到通知fruit_count()。
<?php function fruit_count() { $mango_count = 10; echo 'There are '.$mango_count.' mangoes inside fruit_count().'; } fruit_count(); // There are 10 mangoes inside fruit_count(). echo 'There are '.$mango_count.' mangoes here.'; // Notice about Undefined Variable ?>
访问函数内部的变量
函数通常需要一些输入数据来做一些有用的事情。最好的方法是在函数定义中使用不同的参数传递信息。例如,natural_sum()下面的函数将自然数的个数作为其参数。
<?php function natural_sum($n) { $sum = $n*($n + 1)/2; echo 'The sum of first '.$n.' natural numbers is '.$sum.'.'; } natural_sum(99); // The sum of first 99 natural numbers is 4950. ?>
如果需要从外部获取变量并在函数内部使用,可以考虑使用global关键字。
使用 PHPglobal关键字访问变量
您可以简单地global在任何变量之前使用关键字来从全局范围获取其值。当您这样做时,存储在变量中的任何内容都将在函数内部可用,而无需将其作为参数传递。您还可以更改函数内部变量的值,并且更改将反映在任何地方。
这是一个重写natural_sum()函数以使用全局值的示例$n。
<?php $n = 99; function natural_sum() { global $n; $sum = $n*($n + 1)/2; echo 'The sum of first '.$n.' natural numbers is '.$sum.'.'; } natural_sum(); // The sum of first 99 natural numbers is 4950. ?>
以这种方式做事的主要缺点之一是我们不再知道natural_sum()依赖哪种数据来生成其输出。的值$n也可以在代码的任何地方更改,它会影响natural_sum()再次调用的输出。
<?php $n = 99; function natural_sum() { global $n; $sum = $n*($n + 1)/2; echo 'The sum of first '.$n.' natural numbers is '.$sum.'.'; $n += 5; } natural_sum(); // The sum of first 99 natural numbers is 4950. natural_sum(); // The sum of first 104 natural numbers is 5460. ?>
natural_sum()在上面的示例中,如果不查看代码,我们无法知道是什么改变了输出。我们仍然可以很容易地发现,$n函数本身的值增加了 5。但是,当您处理大量代码时,这可能并不容易。
这就是为什么最好尽可能避免使用全局变量。
使用 PHP$GLOBALS超全局变量访问变量
在函数内部访问全局变量的另一种方法是使用$GLOBALS超全局变量。这也允许您拥有一个与全局变量同名的局部变量。
<?php $n = 99; function natural_sum() { $sum = $GLOBALS['n']*($GLOBALS['n'] + 1)/2; echo 'The sum of first '.$GLOBALS['n'].' natural numbers is '.$sum.'.'; $n = 20; $sum = $n*($n + 1)/2; echo 'The sum of first '.$n.' natural numbers is '.$sum.'.'; } natural_sum(); // The sum of first 99 natural numbers is 4950. // The sum of first 20 natural numbers is 210. ?>
使使用global关键字成为一种不好的做法的问题也存在$GLOBALS。
您应该考虑尽可能避免使用全局变量,并且仅在您想不出任何其他方法可以轻松地从特定函数内的变量中获取数据时才使用它们。
使用它可能有用的一种情况global是,当您想要跨多个不同的函数定义跟踪和更新变量的值并希望变量值的变化反映在任何地方时。
PHP 中的静态变量
PHP 中函数内部定义的变量只存在于该函数的范围内。这意味着一旦我们退出本地范围,该变量就不再可用。这也意味着我们分配给函数内部变量的任何值都不会在对该函数的多次调用中持续存在。
<?php function natural_sum($n) { $count = 0; $sum = $n*($n + 1)/2; echo 'The sum of first '.$n.' natural numbers is '.$sum.'.'; $count += 1; echo 'natural_sum() have been called '.$count.' time(s).'; } natural_sum(10); // The sum of first 10 natural numbers is 55.natural_sum() have been called 1 time(s). natural_sum(30); // The sum of first 30 natural numbers is 465.natural_sum() have been called 1 time(s). natural_sum(45); // The sum of first 45 natural numbers is 1035.natural_sum() have been called 1 time(s). ?>
如您所见,我们natural_sum()在上面的示例中调用了 3 次,但它的值在我们调用的顶部一直重置为 0。这使我们很难跟踪我们调用的次数natural_sum()。
解决此限制的一种方法是使用static关键字。现在, 的值$count将在多个调用中保持不变,如下面的示例所示。
<?php function natural_sum($n) { static $count = 0; $sum = $n*($n + 1)/2; echo 'The sum of first '.$n.' natural numbers is '.$sum.'.'; $count += 1; echo 'natural_sum() have been called '.$count.' time(s).'; } natural_sum(10); // The sum of first 10 natural numbers is 55.natural_sum() have been called 1 time(s). natural_sum(30); // The sum of first 30 natural numbers is 465.natural_sum() have been called 2 time(s). natural_sum(45); // The sum of first 45 natural numbers is 1035.natural_sum() have been called 3 time(s). echo $count; // Notice about Undefined Variable ?>
上面的示例还表明,在 PHP 中将变量声明为静态不会使其在其范围之外可用。
如果要在函数之外访问变量的值,最好的方法是使用return语句。您可以返回任何类型的值,例如字符串、数组、对象等。
<?php function natural_sum($n) { static $count = 0; $sum = $n*($n + 1)/2; echo 'The sum of first '.$n.' natural numbers is '.$sum.'.'; $count += 1; return $count; } natural_sum(10); natural_sum(30); $count = natural_sum(45); echo 'natural_sum() was called '.$count.' time(s).'; // natural_sum() was called 3 time(s). ?>
最后的想法
PHP 有两种变量作用域。有一个通用的全局范围,您在程序中定义的变量在所有地方都可用,例如循环和其他包含带有变量的文件的文件。然后是用户定义函数的局部作用域,其中变量仅限于该特定函数。
然而,这并不妨碍函数内外的信息自由移动。您可以使用global关键字访问未作为参数包含在函数中的外部变量。同样,您可以使用return语句访问函数内部定义的变量的值。
- 使用 PHPglobal关键字访问变量
- 使用 PHP$GLOBALS超全局变量访问变量