PHP Variable Scope

PHPServer Side ProgrammingProgramming

Introduction

In programming, scope refers to the extentto which a variable is accessible. Generally, a simple PHP script (without any constructs such as loop, function etc.) has a single scope, in the sense, a variable is available througout the program from the point of definition onwards.

Variable in a main script is also made available to any other script incorporated with include or require statements. In following example, a test.php script is included in the main script.

This is the main script

$var=100;
include "test.php";
?>

The included file test.script is as follows −

echo "value of of \$var in testscript.php : " . $var;
?>

When main script is executed, following result is displayed

value of of $var in testscript.php : 100

However, when script has a user defined function, any variable inside has a local scope. As a result, variable defined inside a funcion can't be accessed outside. Variable defined outside (above) the function has a global scope.

Example

 Live Demo

<?php
$var=100; //global variable
function myfunction(){
   $var1="Hello"; //local variable
   echo "var=$var var1=$var1" . "\n";
}
myfunction();
echo "var=$var var1=$var1" . "\n";
?>

Output

This will produce following result −

var= var1=Hello
var=100 var1=
PHP Notice: Undefined variable: var in line 5
PHP Notice: Undefined variable: var1 in line 8

Note that global variable is not automatically available within local scope of function. Also, variable inside function is not accessible outside

global keyword

Access to global variable inside local scope should be explicitly enabled by using global keyword. The PHP script is as follows −

Example

 Live Demo

<?php
$a=10;
$b=20;
echo "before function call a = $a b = $b" . "\n";
function myfunction(){
   global $a, $b;
   $c=($a+$b)/2;
   echo "inside function a = $a b = $b c = $c" . "\n";
   $a=$a+10;
}
myfunction();
echo "after function a = $a b = $b c = $c";
?>

Output

This will produce following result −

before function call a = 10 b = 20
inside function a = 10 b = 20 c = 15
PHP Notice: Undefined variable: c in line 13
after function a = 20 b = 20 c =

Global variable can now be processed inside function. Moreover, changes made to global variable inside function will be reflected in global namespace

$GLOBALS array

PHP stores all global variables in an associative array called $GLOBALS. Name and value of variable forms the key-value pair

In the following PHP script, $GLOBALS array is used to access global variables −

Example

 Live Demo

<?php
$a=10;
$b=20;
echo "before function call a = $a b = $b" . "\n";
function myfunction(){
   $c=($GLOBALS['a']+$GLOBALS['b'])/2;
   echo "c = $c" . "\n";
   $GLOBALS['a']+=10;
}
myfunction();
echo "after function a = $a b = $b c = $c";
?>

Output

before function call a = 10 b = 20
c = 15
PHP Notice: Undefined variable: c line 12
Notice: Undefined variable: c in line 12
after function a = 20 b = 20 c =

static variable

A variable defined with static keyword is not initialized at every call to the function. Moreover, it retains its value of previous call

Example

 Live Demo

<?php
function myfunction(){
   static $x=0;
   echo "x = $x" . "\n";
   $x++;
}
for ($i=1; $i<=3; $i++){
   echo "call to function :$i : ";
   myfunction();
}
?>

Output

This will produce following result

call to function :1 : x = 0
call to function :2 : x = 1
call to function :3 : x = 2
raja
Published on 19-Sep-2020 18:39:18
Advertisements