CODING ACADEMY

View Original

How to use PHP $GLOBALS

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:

See this content in the original post

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

See this content in the original post

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

See this content in the original post

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

See this content in the original post

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