make money free online
star business online
12:05 AM

Global Variables

Posted by Business

this time online business to explain about global variable, what is global variable let's
we see. 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. Consider an example:

$somevar = 15;

function addit() {

GLOBAL $somevar;

$somevar++;

print "Somevar is $somevar";

}

addit();

The displayed value of $somevar would be 16. However, if you were to omit this
line:

GLOBAL $somevar;


An alternative method for declaring a variable to be global is to use PHP's $GLOBALS
array. Reconsidering the above example, I use this array to declare the variable
$somevar to be global:

$somevar = 15;

function addit() {

$GLOBALS["somevar"];

$somevar++;
}
addit();

print "Somevar is $somevar";

0 comments:

Post a Comment