jump to navigation

Global Variables & superglobals in PHP October 19, 2010

Posted by Tournas Dimitrios in PHP.

Welcome to another part of the serie ” The basics of OOP with PHP ” follow up …

Global variables are set by a PHP script author to assure that a variable and its value will be available inside of a function, or when another PHP file is called to manipulate that variable. When you create custom variables in the normal fashion those variables and their values will not be available to you inside of a function or an externally called file. To make sure they are available you create the variable in the normal way and then apply the following code to make global and available to cover more scope only when needed.

// Define your variable and its value
$flavor1 = "chocolate";
// Now set it as global
global $flavor1;
//-------------------- EXTRA INFO ---------------------------------
// You can also set many variables as global together by using:
global $flavor1, $flavor2, $flavor3;

Here is a code example showing where and why applying global to a variable is handy. 

// Define your variable and its value 
$name1 = "Obama"; 
// Create a simple function that will display a sentence 
function sampleFunction() { 
    // Set $name1 variable as global 
    global $name1; 
    // Now echo a sentence to the browser 
    echo "Hello $name1, welcome to my  web page."; 

// This is how you can execute a function to run, we talk more about functions later 
sampleFunction(); //Hello Obama, welcome to my web page.


Predefined Variables (aka: superglobals)
There are a set of predefined variables in PHP that are always available for use in any script or scope in your applications. When data is created, sent, or stored using mechanisms that create superglobal variables, we can then access those variables sitewide or all throughout our scripts and scope. Predefined superglobals are available to all of your scripts that are part of a website or application you are creating.

The superglobal variables are:

$_GET Stores any variables created using the GET method
$_POST Stores any variables created using the POST method
$_REQUEST Stores any variables created through a user input script (it can access both POST or GET)
$_FILES Stores any file upload variables created through user input scripts
$_SESSION Stores any variables created through registering session variables
$_COOKIE Stores any variables created through setcookie
$GLOBALS Stores any variables that have been globally defined
$_SERVER Stores server information such as headers, file names, reference paths, and current page.
$_ENV Stores any variables associated with the server environment.

Here is an example of accessing superglobal variables for the server:

//Obtain user IP address  
$ip = $_SERVER['REMOTE_ADDR'];  
// Obtain browser 
$browser = $_SERVER['HTTP_USER_AGENT']; 
// Obtain user system language 
// Obtain the URL of the page that they came from 
$referingURL = $_SERVER['HTTP_REFERER']; 
// Obtain the page they are currently on 
$currentPage = $_SERVER['REQUEST_URI']; 

// Now show all of that information on page 
echo "$ip <br />"; 
echo "$browser <br />";  
echo "$language <br />"; 
echo "$referingURL <br />";  
echo "$currentPage <br />"; 


No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s