How to use PHP $GLOBALS

phplogo.jpg

There are several really useful super globals in PHP. They are as follows:

  • $GLOBALS

  • $_SERVER

  • $_GET

  • $_POST

  • $_FILES

  • $_COOKIE

  • $_SESSION

  • $_REQUEST

  • $_ENV

In this lesson we shall take a look at the first of these: $GLOBALS.

As you may be aware, you can use variables within a function as follows:

The variables are only accessible from within the function and cannot be used outside of it. For example, the following would not work:

We can can the super global variables $GLOBALS to access variables outside of the scope of the function as follows:

We can also use variables from outside the scope of the function as follows:

Did you find this lesson useful? Learn PHP by following one of our courses.