Categories
PHP

Variable Scope

How to use use variables declared outside the function?

  1. Local function scope
  2. Using global keyword
  3. Using $GLOBALS superglobal

Local function scope

Variables used inside a function are different from those used outside a function. The variables used inside the function are limited to the scope of the function. Following example may generates a warning (depends on your PHP settings) and no value for $temp variable:

<?php
 function doublevalue($var) {
  $temp = $var * 2;
 }
 $variable = 5;
 doublevalue($variable);
 echo '$temp is: '.$temp;
 # Warning: Undefined variable $temp...
 # $temp is: 

The scope of the variable $temp is local to the function doublevalue() and is discarded when the function returns. The PHP script engine doesn’t complain about the undeclared variables being used. It just assumes the variable is empty.

Similary, the variables declared outside a function can not use inside a function, see following example:

<?php
 $temp = 'BrainBell.com';
 function demo() {
  echo '$temp = '. $temp;
 }
 demo();

 # Warning: Undefined variable $temp...
 # $temp = 

Above example also generates a warning (depends on your PHP settings) and no value for $temp variable.

You could have still used the variable name $temp inside a function, however, the $temp inside the function is a different variable from the $temp outside the function.

<?php
 $temp = 10;
 function printSomething() {
  $temp = 50;
  echo '$temp inside function: '. $temp . '<br>';
 }
 printSomething();
 echo '$temp outside function: '. $temp . '<br>';
 
 # $temp inside function: 50
 # $temp outside function: 10

The general rule is that variables used exclusively within functions are local to the function, regardless of whether an identically named variable is used elsewhere. There are two exceptions to this general rule: variables passed by reference and those declared global in the function aren’t local to the function.

Using global keyword

If you want to use the same variable everywhere in your code, including within functions, you can do so with the global statement. The global statement declares a variable within a function as being the same as the variable that is used outside of the function. Consider this example:

Example: Access variables inside or outside a function

<?php
 function doublevalue() {
  global $temp;
  $temp = $temp * 2;
 }
 $temp = 5;
 doublevalue();
 echo $temp; # Prints: 10

Because $temp is declared inside the function as global, the variable $temp used in doublevalue() is a global variable that can be accessed outside the function. Because the variable $temp can be seen outside the function, the script prints 10.

Note: The global keyword can be used with multiple variable names at once. Just separate each variable name with a comma:

<?php
 $a = 5;
 $b = 10;
 $c = 15;
 testMultiGlobal();
 function testMultiGlobal() {
  global $a, $b, $c;
  echo "$a , $b , $c";
 }
 # 5 , 10 , 15

Using $GLOBALS array

The $GLOBALS is a superglobal variable that is always available in all scopes. It is an associative array with the name of the global variable being the key and the contents of that variable being the value of the array element. Consider following example:

<?php
 function testing() {
  echo ' $a = '. $GLOBALS['a'];
  echo ' $b = '. $GLOBALS['b'];  
  echo ' $c = '. $GLOBALS['c'];
 }
 $a = 10;
 $b = 11;
 $c = 12;
 testing();
 # $a = 10 $b = 11 $c = 12

User-defined functions:

  1. Defining functions
  2. Passing variables by reference
  3. Using arrow and anonymous functions
  4. Understanding variable scope
  5. Including external files