Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding the PHP $GLOBALS variable

I am learning PHP from w3schools' PHP Tutorial.

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

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

<!DOCTYPE html>
<html>
  <body>

  <?php
    echo "<pre>";
    print_r($GLOBALS);
    echo "</pre>";
  ?>

  </body>
</html>

I got following output in a browser :

Array
(
    [_GET] => Array
        (
        )

    [_POST] => Array
        (
        )

    [_COOKIE] => Array
        (
            [toWorkNormally] => 1
        )

    [_FILES] => Array
        (
        )

    [GLOBALS] => Array
 *RECURSION*
)

The above output has created many doubts in my mind as follows :

  1. As per my knowledge in PHP there are nine types of superglobals (predefined PHP global variables) viz. $GLOBALS, $_SERVER, $_REQUEST, $_POST, $_GET, $_FILES, $_ENV, $_COOKIE and $_SESSION then my doubt is what does the array elements from the predefined global array $GLOBALS viz. [_GET], [_POST], [_COOKIE], [_FILES] mean as they have their own independent existence as superglobals?
  2. What is meant by [toWorkNormally] => 1 from above array output?
  3. What does mean by RECURSION in element [GLOBALS] and how to print those elements?
  4. As the purpose of $GLOBALS array is to store variables declared by user globally then how this array has been pre-populated with some other values as I haven't declared any global variable in my code?

Note : I'm using "Microsoft Windows 10 Home Single Language" operating system on my machine. It's a 64-bit Operating System. I'm using latest edition of XAMPP with PHP 7.0.13 and HTTP Apache web server v.2.4.23 for running the program locally. Also, please note that I have not defined any other variable as global or local in my code.

like image 444
PHPFan Avatar asked Jan 07 '17 18:01

PHPFan


People also ask

What are the 4 variable scopes of PHP?

PHP has three different variable scopes: local. global. static.

Which are the Superglobal variable in PHP?

The PHP superglobal variables are: $GLOBALS. $_SERVER. $_REQUEST.

What is global variable with example in PHP?

Global variables refer to any variable that is defined outside of the function. Global variables can be accessed from any part of the script i.e. inside and outside of the function. So, a global variable can be declared just like other variable but it must be declared outside of function definition.

How do PHP variables work?

A variable starts with the $ sign, followed by the name of the variable. A variable name must start with a letter or the underscore character. A variable name cannot start with a number. A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )


1 Answers

From my knowledge of PHP and doing some research as well as testing this on multiple OS' with various version of PHP I found the following.

Question 1 & 3:

Yes you are correct with regards to the 9 superglobals, but a very important thing to keep in mind is that $GLOBALS -- References all variables available in global scope.

An interesting sidenote, notice that $GLOBALS is the only superglobal that doesn't start with an underscore.

Because of the fact that $GLOBALS contains references to all the other superglobals including itself, when we print_r($GLOBALS) it will also include the other superglobals in the output. Because $GLOBALS references itself as well we get the RECURSION you asked about in your 3rd point. You can think of it as a infinite dimensional array containing $GLOBALS. Almost the same idea as an infinte loop.

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

Instead the script sees this and stop executing and just prints RECURSION. Now I have tested it on 3 different environments and each time the order in which the superglobals are printed changed, but as soon as it hits $GLOBALS it stops and prints RECURSION.

Question 2:

I could not find any info on $_COOKIE[toWorkNormally] => 1. I am assuming this is set somewhere else. I didn't see it in any of my tests.

Question 4:

This is neither correct nor incorrect. The purpose of $GLOBALS is not to store all variables created by the user globally. It merely references all variables available in global scope including, the superglobals. That is why you are seeing all the other superglobals in the output. But a lot of developers assume that the user defined global variables are stored in $GLOBALS.

Description in the PHP.net manual

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.

To view all the superglobals you will have to print_r() each one of them individually.

To check all user defined global variables you can use array_keys($GLOBALS) all the items which are not superglobals will most probably be user defined global variables.

EDIT in response to users comments

In response to your 1st comment, No they are not different. The superglobals not printed are still part of the array but execution/output stops because it hits the RECURSION when it gets to $GLOBALS. The superglobals are printed in a random order and which ever comes after the $GLOBALS will not be seen as it detects a RECURSION at $GLOBALS and stops the output.

You can check all the superglobals/global variables by using print_r(array_keys($GLOBALS)); With an exception of $_SESSION because a session has not been started yet. print_r($_SESSION) will give you an undefined variable $_SESSION Notice. You will be able to see $_SESSION when you put session_start(); just before you print it.

Link to What References Are in PHP

References in PHP are a means to access the same variable content by different names.

Note that in PHP, variable name and variable content are different, so the same content can have different names

like image 79
J2D8T Avatar answered Sep 20 '22 02:09

J2D8T