Tuesday, 20 January 2015

Global Variable in PHP

PHP Global variable is declared outside the function. It can also be declared inside the function with the keyword "GLOBAL". It can be accessed from any part in the program.

Syntax:


global varname;

In the above syntax, global is a keyword. varname is the variable name.

Example:


<?php
function doublevalue()
{
global $tmp;
$retval=7*$tmp;
return $retval;
}
$tmp=5;
$dispval=doublevalue();
echo $dispval;
?>

Result


35

In the above example, $tmp is declared as a global variable. So, it can be accessed outside the doublevalue() function, even if it is declared inside the function.

0 comments:

Post a Comment