PHP Global Variables

Description

A global variable can be accessed anywhere in your script, whether inside or outside a function.

Syntax

In PHP, all variables created outside a function are, in a sense, global in that they can be accessed by any other code in the script that's not inside a function.

To use such a variable inside a function, write the word global followed by the variable name inside the function ' s code block.


<?PHP/*  ww  w  . ja v a  2  s. co m*/
         $myGlobal = "Hello there!"; 

         function hello() { 
            global $myGlobal; 
            echo "$myGlobal\n"; 
         } 

         hello(); // Displays "Hello there!"  
?>

The code above generates the following result.

hello() function accesses the $myGlobal variable by declaring it to be global using the global statement. The function can then use the variable to display the greeting.

Example 1

We don't need to have created a variable outside a function to use it as a global variable. Take a look at the following script:


<?PHP//from w  w w  . j  av  a  2  s.c om
         function setup() { 
           global $myGlobal; 
           $myGlobal = "Hello there!"; 
         } 

         function hello() { 
           global $myGlobal; 
           echo "$myGlobal\n"; 
         } 

         setup(); 
         hello(); // Displays "Hello there!"   
?>

The code above generates the following result.

In this script, the setup() function is called first. It declares the $myGlobal variable as global, and gives it a value.

Then the hello() function is called. It too declares $myGlobal to be global, which means it can now access its value previously set by setup() and display it.

Example 2

The $GLOBALS array can access global variables within functions. All variables declared in the global scope are in the $GLOBALS array, which you can access anywhere in the script. Here is a demonstration:


<?PHP//w  ww.j a va2  s  . co  m
function foo() { 
   $GLOBALS['bar'] = "java2s.com"; 
} 

$bar = "PHP"; 
foo(); 
print $bar; 
?>

The code above generates the following result.

We can read variables in the same way:


$localbar = $GLOBALS['bar']; 

PHP GLOBAL keyword allow a variable to be accessed locally.


function myfunc() {                                                                            
    GLOBAL $foo, $bar, $baz;                                                               
    ++$baz;                                                                                
} 

The code above reads the global variables $foo, $bar, and $baz. The ++$baz line will increment $baz by 1, and this will be reflected in the global scope.

Note

We can also declare more than one global variable at once on the same line, just separate the variables using commas:


function myFunction() { 
  global $oneGlobal, $anotherGlobal; 
}   

Be careful with global variables. If you modify the value of a global variable in many different places within your application, it can make it hard to debug your code.

Generally speaking, you should avoid using global variables unless it's strictly necessary.





















Home »
  PHP Tutorial »
    Language Basic »




PHP Introduction
PHP Operators
PHP Statements
Variable
PHP Function Create
Exception
PHP Class Definition