PHP

PHP Super Globals


Superglobals are built-in variables in PHP that are always available in all scopes. Available in all scopes mean these variables can be accessed from any part of your application/project. These are the super global variables we have in PHP at the time of this writing:

1. $GLOBALS

Any variable assigned to this super global is accessible in any PHP script in your application. For example, let’s assume we have 2 php files like so:

index.php

claim.php

2. $_ENV

An associative array of variables passed to the current script via the environment method.

Essentially, there are some variables that you want access to in your development environment, test or production environment. This superglobal helps you to return environment variables from the web server.

For example, you can insert values into the $_ENV array like so:

 

You can also just try to print out the values in $_ENV array.

 

On my localhost machine where I actually use Vagrant and Laravel Homestead. I actually see something like so:

They are there because I defined them when I was setting up my application.

3. $_SERVER

$_SERVER is an array containing information such as headers, paths, and script locations. The entries in this array are created by the web server. There are predefined keys and values in this array already, they all provide very useful information about your web application, scripts and web server.

Go ahead and run this in a php script

I ran this on a tutorial project I developed here on goodheads called Prego. This is the result:

So if you want to get the IP address of the server, you can just access it like so:

$_SERVER['SERVER_ADDR'] or the route that ran the script $_SERVER['REQUEST_URI'].

4, 5. $_GET, $_POST

$_GET and $_POST  are used to get information sent in the page request. Parameters sent in the URL otherwise known as query strings are made available by $_GET. For example, this URL:

… will send the parameter id to tutorial.php, which you can then access the value in tutorial.php as $_GET["id"].

When you have a form submission that is sent using method="get", the values are appended to the URL so they are available using $_GET. Forms submitted using method="post" send their values in the body of the HTTP request; posted data is available in your script by using $_POST. Here’s a sample web form which posts a username and password to a login script:

The full_name value and msg value will be available as $_POST["full_name"] and $_POST["msg"] respectively in contact.php

6. $_REQUEST

The $_REQUEST superglobal is like an alias. You can use it in place of either $_GET or $_POST.

7. $_SESSION

Sessions are needed to keep track of information about users in a web application as they move from one part of the application to another. So an example of that is this:

Now create another file and name it that_page.php

Once session_start() is called on this_page.php it starts a session and in that_page.php, the session_start() called here resumes the session so that the information is not lost.

8. $_COOKIE

cookie is a small piece of data sent from a website and stored in the user’s web browser while the user is browsing it. Every time the user loads the website, the browser sends the cookie back to the server to notify the user’s previous activity. Cookies were designed to be a reliable mechanism for websites to remember stateful information (such as items added in the shopping cart in an online store) or to record the user’s browsing activity (including clicking particular buttons, logging in, or recording which pages were visited in the past).

$_COOKIE  is a superglobal array that is used to retrieve the values of cookies that have been set using the setcookie function.

Let’s set a cookie like so:

If you check your browser cookies, you will see a cookie with these details above.

9. $_FILES

This is an associative array of items uploaded to the current script via the HTTP POST method. It contains information about files you have uploaded via a form.

Create an index.php file like so:

Create a process_upload.php file like so:

Make sure you select a file, once you click submit, it redirects you to process_upload.php and dumps everything in $_FILES.

Please let me know if you have any questions or observations in the comments section below :smile:

 

PROSPER OTEMUYIWA

About PROSPER OTEMUYIWA

Food Ninja, Code Slinger, Technical Trainer, Accidental Writer, Open Source Advocate and Developer Evangelist.