PHP Global Variables

In this chapter you will learn:

  1. What is PHP global variable
  2. How to create a global variable
  3. Example
  4. Example - $GLOBALS Array
  5. Note for 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/*j  av  a2 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/* ja v  a2 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//  j  a  v a2s. c  o 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.

Next chapter...

What you will learn in the next chapter:

  1. What is a recursive function
  2. How to write recursive functions
  3. Example - A PHP recursive function
Home » PHP Tutorial » PHP Function Create
PHP Function
PHP Function Create
PHP Function Parameter
PHP Function Default Parameters
PHP Variable Length Parameter
PHP Function Reference Parameter
PHP Function Return
PHP Return Reference
PHP Variable Scope in Functions
PHP Global Variables
PHP Recursive Functions
PHP Anonymous Function
PHP static variable