Sunday, March 23, 2014

Global Variable in PHP

/* In contrast to local variables, a global variable can be accessed in any part of the program. However, in order to be modified, a global variable must be explicitly declared to be global in the function in which it is to be modified. This is accomplished, conveniently enough, by placing the keyword GLOBAL in front of the variable that should be recognized as global. Placing this keyword in front of an already existing variable tells PHP to use the variable having that name. */

<?php
$a=5;
$b=3;

function mahi()
{
$c=$GLOBALS['a']+$GLOBALS['b'];
echo $c;
}
mahi();
?>

Output :-



No comments:

Post a Comment