Forum : What is Global and Local variable ?
Brief description  about Online courses   join in Online courses
View Balu  Prasad 's Profile

What is Global and Local variable ?

Global and Local variables.....
Asked by Balu Prasad | Mar 17, 2010 |  Reply now
Replies (1)
View teacher siliconindia 's Profile
Hi Balu.
In PHP, any variable declared outside a function as $a above, is a global variable. In PHP any variable declared inside a function (see below), is a local variable. In the following code, the $a declared outside the function and the $a declared inside the function are entirely two different things. Read and try the following code
<?php
$a = 4;
function sendValue()
{
$a;
echo $a;
}
echo "Value of variable outside<br />";
echo $a; echo "<br />";
echo "Value of variable inside<br />";
sendValue();
?>
As you can see from the result, the two variables, though having the same name, but by the fact that one is outside the function and the other is inside, would hold different values. The one inside the function in this case, did not even acquire a value.
Mar 17, 2010