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.
<?php
// 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.
<?php
// Define your variable and its value
$name1 = "Joe";
// 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 Joe, 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:
<?php
//Obtain user IP address
$ip = $_SERVER['REMOTE_ADDR'];
// Obtain browser
$browser = $_SERVER['HTTP_USER_AGENT'];
// Obtain user system language
$language = $_SERVER['HTTP_ACCEPT_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 />";
?>
Superglobal variables are created and defined in different ways. We will
demonstrate these methods throughout our lessons, we felt that while
we are discussing variables we should tell you about all of the
different types available to you and your scripts.