gpt4 book ai didi

php - GLOBALS 和 GLOBAL 有什么区别?

转载 作者:可可西里 更新时间:2023-11-01 13:07:29 26 4
gpt4 key购买 nike

在 PHP 中,我想知道 GLOBAL 和 GLOBALS 之间的区别。

一些例子:

print_r($GLOBALS);

最佳答案

这是与同一事物相关的两个不同事物:全局变量。

$GLOBALS - PHP superglobal表示可作为数组访问的全局变量表的数组。因为它是超全局的,所以它随处可用。

An associative array containing references to all variables which are currently defined in the global scope of the script. The variable names are the keys of the array.

global - 将特定全局变量导入局部变量表的关键字。


然后你问:

But why we cant access the session and cookie variables by using $GLOBALS?

错了,您可以使用 $GLOBALS 访问 session 和 cookie 变量:

$GLOBALS['_SESSION']['session_variable_name']

但是$_SESSION也是超全局变量,因此您不需要使用 $GLOBALSglobal 从任何地方访问 session 变量:

$_SESSION['session_variable_name']

同样适用于 $_COOKIE .

关于php - GLOBALS 和 GLOBAL 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8035355/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com