Are there any differences between $ GLOBALS ["test"] and global $ test?

can someone please let me know the main differences between

$GLOBALS["test"] and global $test 

and does it make sense if I use $GLOBALS["test"] instead of $_SESSION['test'] ?

+1
source share
2 answers

and does it make sense that if I use $ GLOBALS ["test"] instead of $ _SESSION ['test']?

No, session is different from a variable available worldwide.

$ GLOBALS

An associative array containing references to all the variables that are currently defined globally from the script. Variable names are the keys of the array.

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

Explanation:

$GLOBALS - associative array available throughout your script, no need to use global $test

Note: this is a "superglobal", or automatic global, variable. This simply means that it is available in all areas within the script. There is no need to make a global variable $; to access them as part of functions or methods.

+4
source

There is no difference between $GLOBALS["test"] and global $test . Both are pure evil and should not be used.

Why are they evil?

  • Suddenly, your code will depend on some external environment, its portability falls on the heels. This requires some variable defined somewhere, no one knows where, with some value, no one knows what the correct value is.
  • Suppose that $test should store information about something, let them say: the number of balls. Everything is fine until there is such a variable, and it saves what it should store. However, what happens if you decide to delete this variable or use it for other purposes? Bah, fatal errors come out of nowhere! You do not know what is happening, everything works fine, you just change the value of the variable, and everything falls apart.
+1
source

Source: https://habr.com/ru/post/900928/


All Articles