#native_company# #native_desc#
#native_cta#

Using Global Variables in PHP

By Joydip Kanjilal
on December 3, 2014

A variable is an object that holds value. The scope of a variable in PHP is limited to the context in which it is defined. Scope of a variable defines the accessibility and durability of it. PHP variables can be defined in anyone of the following four scopes:

   • Local variables

   • Function parameters

   • Global variables

   • Static variables

In this article we will explore global and super global variables in PHP. Before we delve deep into global variables in PHP, let’s take a quick tour of the basics.

PHP Variables

You can define a variable in PHP by preceding the variable name by a $ sign. Here is an example:

<?php
$textvar = "Hello world!";
$numericVar = 1;
$decimaVar = 1.2;
?> 

Scope of a variable defines the durability and accessibility of it. As an example the $myVar variable provided in the PHP script below is accessible from within the test.inc script only.

<?php
$myVar = 1;
include 'test.inc';
?> 

Variables defined globally have global scope. On the contrary variables that are local to a function are limited to the scope of that function in which they are defined.

<?php
$numericVar = 1; /* This variable is in the global scope */ 
function test()
{ 
    echo $numericVar;
} 

test();
?> 

When you execute the above script, it wouldn’t produce any output. The reason is that you have declared a variable in the local scope that hasn’t been assigned any value. If you are to access a global variable from within a function in PHP, you should explicitly specify it as global. Here is another example that illustrates this:

<?php
$numericVar = 1; /* This variable is in the global scope */ 
function test()
{ 
    global $numericVar;
    echo $numericVar;
} 
test();
?> 

While declaring variables in PHP, the following points should be noted:

   • The name of a variable should start with the $ sign

   • The name of a variable should start with a letter or the underscore character

   • The name of a variable cannot start with a number

   • The name of a variable can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )

   • Variable names in PHP are case-sensitive. Hence, $myVar and $MYVAR are two different variables in PHP.

PHP Global Variables

A global variable in PHP is one that can be accessed from any part of the program. This is in contrast to a local variable that can be accessed only from within the scope or context in which it is defined. It should be noted that if a global variable needs to be modified it should be explicitly declared global in the function in which it would be modified.

To implement this, you should precede the variable with the keyword GLOBAL. Here is an example that illustrates this:

<?
$globalvar = 1;
function test() {
GLOBAL $globalvar;
$globalvar++;
print "Value of globalvar is $globalvar";
}
test();
?> 

PHP Global Variables – Superglobals

Super global variables in PHP are those variable which are always accessible, regardless of scope are they are in the global scope – hence they are globally accessible. Here’s the list of pre-defined PHP super global variables:

$GLOBALS
$_SERVER
$_REQUEST
$_POST
$_GET
$_FILES
$_ENV
$_COOKIE
$_SESSION

The following code snippet illustrates how you can use super global variables in PHP.

<?php 
$numericVar = 1; 
$anotherNumericVar = 2;
 function add() { 
    $GLOBALS['result'] = $GLOBALS['numericVar'] + $GLOBALS['anotherNumericVar']; 
}
add(); 
echo $result; 
?> 

References

Check the link below for references for further study on this topic.

http://php.net/manual

Summary

Global variables in PHP are used to ensure that the variable can be accessed from any function from that program. To ensure that a variable declared in PHP is globally accessible, you should precede it with the global keyword as shown below:

<?php 
$myVar1 = "Hello";
$myVar2 = "World!";
global $myVar1, $myVar2; 
?> 

In this article we explored global variables in PHP. I will present more interesting topics on PHP in my future articles here. So, stay tuned. Happy reading!