关于php:$ GLOBALS [“ test”]和$ global之间是否有区别?

Is there any differents between $GLOBALS[“test”] and global $test?

可以让我知道两者之间的基本区别吗

1
$GLOBALS["test"] and global $test

并且,如果我使用$GLOBALS["test"]而不是$_SESSION['test']是否有意义?


and, will it make sense that, if i use
$GLOBALS["test"] instead of
$_SESSION['test']?

不,会话与全局可用变量不同。

$全球

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.

http://php.net/manual/en/reserved.variables.globals.php

说明:

$GLOBALS是整个脚本中可用的关联数组,无需使用global $test

Note: This is a 'superglobal', or
automatic global, variable. This
simply means that it is available in
all scopes throughout a script. There
is no need to do global $variable; to
access it within functions or methods.


$GLOBALS["test"]global $test之间没有区别。两者都是纯粹的邪恶,不应使用。

他们为什么邪恶?

  • 突然,您的代码变得依赖于某些外部环境,其可移植性大打折扣。它要求在某个地方定义一些变量,没人知道在哪里有一些值,没人知道什么是正确的值。
  • 假设$test应该存储有关某物的信息,比方说:球数。一切都很好,直到有一个这样的变量并且它存储了要存储的内容。但是,如果您决定删除该变量或将其用于其他目的,该怎么办? ah,致命错误突然冒出来!您不知道发生了什么,一切正常,您只是更改了变量的值,一切都崩溃了。