PHP 全局变量

WBOY
发布: 2024-08-29 12:35:04
原创
962 人浏览过

在任何编程语言中,全局变量是那些在方法或函数外部声明的变量,也可以在函数内部声明。全局变量就像任何其他变量一样,但不同之处在于该范围在应用程序中是全局的。如果我们将任何变量设为全局变量,那么我们就可以从整个应用程序访问该变量,这意味着在脚本内部或外部也是如此。全局变量在任何地方都以相同的方式起作用,正如名称所示,它们对于其他资源来说是全局的。在接下来的部分中,我们将详细讨论这个 PHP 全局变量。

广告 该类别中的热门课程 PHP 开发人员 - 专业化 | 8 门课程系列 | 3次模拟测试

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

语法:

它们可以像任何其他变量一样声明。但要访问它们,我们需要遵循一些标准。首先,我们将了解如何在 PHP 中声明一个可以在应用程序中的任何位置访问的全局变量。见下文;

$variable = your_value;
登录后复制

正如你在这里看到的,我们只需要定义变量名称,但为此,我们使用“$”符号。之后,我们可以为我们想要的变量分配任何值。为了更好地理解,我们将看到一种可以在编码时在程序中使用的练习语法;

例如:

$demoVar = "hello i am global variable";
登录后复制

全局变量在 PHP 中如何工作?

现在我们知道,全局变量被声明为全局的,可以在应用程序的任何地方使用。该变量与 PHP 中的任何其他变量一样。此外,这些变量也可以从函数内部或外部访问。正如我们之前讨论的,我们只是像其他变量一样声明它们,但为了访问它们,我们需要遵循 PHP 定义的一些标准。所以这里我们将讨论如何在函数内部使用它们以及如何声明它们。为了更好地理解,我们将为初学者看一个示例,以更清楚地了解全局变量。但在继续之前,我们将讨论它用于访问全局变量的一些属性,我们还将看到我们存储全局变量的位置。在 PHP 中,它维护一个数组,其中存储我们在应用程序中定义的所有全局变量。通过使用这个数组,我们可以在脚本内外访问这个变量。让我们讨论全局变量的一些要点,以便在应用程序中使用它们,如下所示;

1) 全局数组

在 PHP 中,我们使用数组来访问这个全局变量。与任何其他编程语言一样,它在数组中维护全局变量的历史记录。如果我们想访问数组中的任何特定元素或变量,那么我们必须传递变量的确切名称才能访问它们。让我们看看下面的语法;

语法:

$GLOBALS['variable_name']
登录后复制

正如您在上面的代码行中看到的,我们使用“$GLO​​BALS”关键字来访问它们,后跟方括号。在这个括号内,我们必须给出我们想要访问的变量名称。让我们看一个练习示例以更好地理解,如下所示;

例如:

$GLOBALS['demovar']
登录后复制

2) 在 PHP 的函数内访问它们

如果我们想访问任何函数或方法中的全局变量,那么我们可以使用“global”关键字。在用 this 提到变量名之后,我们可以在整个函数中使用它们,而无需使用 global 关键字。为了更好地理解,我们将看到一种在函数内使用它们的语法,如下所示;

语法:

global $demovar1, $demovar2, $demovar3, $demovar4;
echo $demovar1;
登录后复制

正如您在上面的示例中看到的,我们使用全局关键字来访问函数内的变量。但是一旦定义,我们就可以直接使用它们,而无需使用 global 关键字。现在我们将了解在应用程序中使用全局变量的一些优点,如下所示;

以下是在 PHP 中使用 global 关键字时需要考虑的一些要点;

  • IF you want to define a global variable you can use ‘$’ followed by the name of your variable.
  • This variable can be accessed inside or outside of the functions well but to access them we have to ‘global’ keyword.
  • We access the global variable inside nay thing by using the global array which maintains all the variables defined in the application.
  • But keep in mind that the variable should be unique otherwise it will override like any other programming language.
  • To access the variable directly from the array we have to use ‘$GLOBALS’ keyword followed by the variable name like we access an array by its index.
  • The advantage of using a global variable is that we may require logged in user names everywhere in the application so by the use of it we can store them into the global variable and that can we easily accessed while application. Which will save memory and provide code optimization also.

Examples

In this example, we are declaring a global variables and trying to print and concatenate them. Simple program for beginners to start with a global variables in PHP.

Example #1

Code:

<!DOCTYPE html>
<html>
<body>
<h2 style = " color :red ">Demo on Global variable in PHP !!</h2>
<?php
//decraring global variable
$demovar1 = "Hello  ";
$demovar2 = "world  ";
$demovar3 = "demo  ";
$demovar4 = "global variable  ";
$demovar5 = "!!!";
//printing result here
echo $demovar1.$demovar2.$demovar3.$demovar4.$demovar5;
?>
</body>
</html>
登录后复制

Output:

PHP 全局变量

Example #2

In this example, are trying to access the global variable from the function using an array.

Code:

<!DOCTYPE html>
<html>
<body>
<h2 style = " color :red ">Demo on Global variable in PHP !!</h2>
<?php
//decraring global variable
$demovar1= "Hello  ";
$demovar2 = "world  ";
$demovar3 = "demo  ";
$demovar4 = "global variable  ";
$demovar5 = "!!!";
// decralrig function
function demoFuntionForglobal() {
echo $GLOBALS['demovar1']."<br>";
echo $GLOBALS['demovar3']."<br>";
echo $GLOBALS['demovar4']."<br>";
echo $GLOBALS['demovar5']."<br>";
}
demoFuntionForglobal();
//printing result here
echo $demovar1.$demovar2.$demovar3.$demovar4.$demovar5;
?>
</body>
</html>
登录后复制

Output:

PHP 全局变量

Conclusion

By using the global variable we can access variables in our whole application these are useful when we have to use same value in the whole application for example username, password and so many other details depend on the requirement. We just need to follow some standards while accessing them inside the function.

以上是PHP 全局变量的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
php
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!