Variable Scope in php

variable Scope :

The scope of a variable is the context within which it is defined. For the most part all PHP variables only have a single scope.

File name index1.php

<?php
$a = 1; /* global scope */

function test()
{
echo $a; /* reference to local scope variable */
}

test();
?>

output :-

Notice: Undefined variable: a in D:\xampp\htdocs\var\index1.php on line 6

This script will not produce any output because the echo statement refers to a local version of the $a variable, and it has not been assigned a value within this scope


The global keyword

File name index1.php

<?php
$a = 1;
$b = 2;

function Sum()
{
global $a, $b;

$b = $a + $b;
}

Sum();
echo $b; // output 3
?>

File name index1.php

<?php
function Sum()
{
global $a=1; global $b=2;
// here error, we can't declaere global variable in this way. within function
$b = $a + $b;
}

Sum();
echo $b; ?>

GLOBALS

A second way to access variables from the global scope is to use the special PHP-defined $GLOBALS array.

The $GLOBALS array 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. Notice how $GLOBALS exists in any scope, this is because $GLOBALS is a superglobal.

File name index1.php

<?php
$a = 1;
$b = 2;

function Sum()
{
$GLOBALS['b'] = $GLOBALS['a'] + $GLOBALS['b'];
}

Sum();
echo $b;
output : 3
?>

Note :

Using global keyword outside a function is not an error. It can be used if the file is included from inside a function.

static variable

Another important feature of variable scoping is the static variable. A static variable exists only in a local function scope, but it does not lose its value when program execution leaves this scope

<?php
function test()
{
static $a = 0;
echo $a;
$a++;
}
test();
test();
test();
test();
?>
output: 0 1 2 3
Now, $a is initialized only in first call of function and every time the test() function is called it will print the value of $a and increment it.

Static variables with recursive functions

Static variables also provide one way to deal with recursive functions. A recursive function is one which calls itself. Care must be taken when writing a recursive function because it is possible to make it recurse indefinitely. You must make sure you have an adequate way of terminating the recursion.

<?php
function test()
{
static $count = 0;

$count++;
echo $count;
if ($count < 10) {
test();
}
$count--;
}
test();


?>
output: 12345678910

what is difference between $(single doller) and $$(double doller).

The $k is the normal variable that stores any value.

The $$k is a reference variable that stores the value which can be accessed by using $ symbol before $k value.

$k = "sana";
$$k = "hello mahi";
echo "$k ";
echo "$$k;";
echo $sana;



########################


<?php
$k = "sana";
$$k = 700;
echo $k."<br/>";
echo $$k."<br/>";
echo $sana;
?>

Output :-

sana
hello mahi
hello mahi



sana
700
700





Previous Next


Trending Tutorials




Review & Rating

0.0 / 5

0 Review

5
(0)

4
(0)

3
(0)

2
(0)

1
(0)

Write Review Here