PHP Tutorial - PHP Functions Scope






Variables declared outside of functions and classes are global. global variables are available else where in the script.

Function variables are self-contained and do not affect variables in the main script.

Variables from the main script are not implicitly made available inside functions.

Example

Take a look at this example:


<?PHP
function foo() { 
   $bar = "java2s.com"; 
} 
$bar = "PHP"; 
foo(); 
print $bar; 
?>

The code above generates the following result.

Execution of the script starts at the $bar = "PHP" line, and then calls the foo() function.

foo() sets $bar to java2s.com, then returns control to the main script where $bar is printed out.

Function foo() is called, and, having no knowledge that a $bar variable exists in the global scope, creates a $bar variable in its local scope.

Once the function ends, all local scopes are gone, leaving the original $bar variable intact.





PHP Global Variables

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

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//from  w ww.j av  a  2 s. c o 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//w ww. j  a v a2  s.c o m
         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
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.