Discussion board
What is Global and Local variable ?
By Balu Prasad
Global and Local variables.....
Reply
Post   Reset
Teacher SiliconIndia replied to Balu Prasad Wednesday, March 17, 2010
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.