Understanding the PHP $ GLOBALS Variable

I am learning PHP from the w3schools PHP tutorial .

While learning PHP, I came across the concept of predefined global variables, i.e. superglobal.

In curiosity, to more deeply understand "Superglobals" I wrote the following code and executed it in a browser on my local machine (ielocalhost):

<!DOCTYPE html> <html> <body> <?php echo "<pre>"; print_r($GLOBALS); echo "</pre>"; ?> </body> </html> 

I got the following output in a browser:

 Array ( [_GET] => Array ( ) [_POST] => Array ( ) [_COOKIE] => Array ( [toWorkNormally] => 1 ) [_FILES] => Array ( ) [GLOBALS] => Array *RECURSION* ) 

The above result caused me many doubts:

  • According to my knowledge, there are nine types of superglobal in PHP (predefined global PHP variables), namely: $GLOBALS, $_SERVER, $_REQUEST, $_POST, $_GET, $_FILES, $_ENV, $_COOKIE and $_SESSION , then mine the doubt is that the elements of the array are from the predefined global array $GLOBALS namely. [_GET], [_POST], [_COOKIE], [_FILES] mean that they are their own independent existence as superglobals?
  • What does [toWorkNormally] => 1 mean from the output of the array above?
  • What does RECURSION mean in the [GLOBALS] element and how to print these elements?
  • Since the purpose of the $GLOBALS array is to store variables declared globally by the user, how was this array pre-populated with some other values ​​since I did not declare any global variable in my code?

Note. I am using the Microsoft Windows 10 Home Single Language operating system on my machine. This is a 64-bit operating system. I use the latest edition of XAMPP with PHP 7.0.13 and the Apache v.2.4.23 HTTP server to run the program locally. Also, note that I have not defined any other variable as global or local in my code.

+8
arrays php global-variables global superglobals
source share
4 answers

From my knowledge of PHP and some research, as well as testing this on multiple OSs using different versions of PHP, I found the following.

Question 1 and 3:

Yes, you are right about 9 superglobal, but it is very important to remember that $ GLOBALS - Links are all variables available in the global area.

An interesting alert, note that $ GLOBALS is the only superglobal that does not start with an underscore.

Due to the fact that $ GLOBALS contains links to all other superglobals, including itself, when we print r ($ GLOBALS), it will also include other superglobals at the output. Since $ GLOBALS also refers to us, we get the RECORD that you asked about in your third paragraph. You can think of it as an infinite dimensional array containing $ GLOBALS. Almost the same idea as the infinte loop.

 [GLOBALS] => Array ( [GLOBALS] => Array ( [GLOBALS] => Array ( ... ) ) ) 

Instead, the script sees this and stops execution and simply prints RECURSION. Now I tested it in three different environments, and every time the order in which superglobals are printed changes, but as soon as it gets into $ GLOBALS, it stops and prints RECURSION.

Question 2:

I could not find information about $_COOKIE[toWorkNormally] => 1 . I guess this is set up somewhere else. I have not seen this in any of my tests.

Question 4:

This is neither right nor wrong. The purpose of $ GLOBALS is not to store all variables created by a user worldwide. It simply refers to all the variables available globally, including superglobals. That is why you see all the other superglobals in the exit. But many developers assume that user global variables are stored in $ GLOBALS.

Description in the PHP.net manual

An associative array containing references to all the variables that are currently defined on a global scale script. The variable names are the keys of the array.

To view all the superglobals, you will need to print_r() each one individually.

To check all user-defined global variables , you can use array_keys($GLOBALS) all elements that are not superglobals are more likely to be user-defined global variables.

EDIT in response to user comments

In response to your 1st comment, no, they are not different. Superglobals not printed are still part of the array, but execution / exit stops because it gets into RECURSION when it gets into $ GLOBALS. Superglobals are printed randomly and will ever appear after $ GLOBALS is not considered, as it detects RECURSION in $ GLOBALS and stops the output.

You can check all superglobals / global variables with print_r(array_keys($GLOBALS)); Except for $ _SESSION, because the session is not already running. print_r($_SESSION) will give you an undefined variable $_SESSION notification. You will be able to see $ _SESSION when you put session_start(); immediately before printing.

Link to what is in PHP

Links in PHP are a means of accessing the same variable contents by different names.

Please note that in PHP the name of the variable and the contents of the variable are different, so the same content can have different names

+3
source share

$GLOBALS is the global of all super-global and user variables. For example, if you specified the variable $a = 10; in the $GLOBALS array, you have the key => a pair of values ​​where the key is a and the value is 10. If you want to get something from $GLOBALS , you just need to write it as an array.

Example

 $a = 25; echo $GLOBALS['a']; 

In the above example, the output will be the value $a so 25;

In your example toWorkNormally=>1 this means that you set a cookie named toWorkNormally and with a value of 1 or true

Also, when you submit a form with the get or post method to $GLOBALS['_GET'] or $GLOBALS['_POST'] , you can find your form data as you can get it from super-global $_GET or $_POST

0
source share

The PHP manual says the following about the $GLOBALS variable:

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

This accurately describes what the variable does. This is just a reference to existing variables.

RECURSION you are talking about is a $GLOBALS variable, referring to itself. Since we don’t want PHP to infinitely output the same output and your server crash in this process, there is a built-in fault tolerant device that shows you a RECURSION warning if that is the case.

I would like to add that $GLOBALS is a superglobal or given global variable. This means that it is available in all areas of your script.

Resources

0
source share

1. According to my knowledge in PHP, there are nine types of superglobals (predefined global PHP variables), namely: $ GLOBALS, $ _SERVER, $ _REQUEST, $ _POST, $ _GET, $ _FILES, $ _ENV, $ _COOKIE and $ _SESSION, then mine the doubt is that the elements of the array are from the predefined global $ GLOBALS array, namely. [_GET], [_POST], [_COOKIE], [_FILES] mean they have their own independent existence as superglobals?

From a PHP doc :

Links to all variables available in the global area

This means that you can access superglobal directly or from $GLOBALS , yes, you have two ways to access them.


2. What is meant by [toWorkNormally] => 1 from the array output above?
It is inside $ _COOKIE, so there is a cookie named toWorkNormally with a value of 1 . Additional cookie information


3. What does RECURSION mean in the [GLOBALS] element and how to print these elements? Recursion means that it referes itself, if it was printed, then it will display the contents of $GLOBALS again nested inside GLOBALS , which will cause an infinite loop. To avoid PHP just typing *RECURSION* .


4. Since the purpose of the $ GLOBALS array is to store variables declared globally by the user, how was this array pre-populated with some other values ​​since I did not declare any global variable in my code?

From a PHP doc :

Several predefined variables in PHP are "superglobal", which means that they are available in all areas of the script. There is no need to make a global variable $; to access them within functions or methods.

In other words, $GLOBALS will show you these predefined variables from PHP, as well as the values ​​that you set manually.

0
source share

All Articles