Global Variable in PHP

What is 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.


global varname;

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

Example :

function doublevalue()
global $tmp;
return $retval;
echo $dispval;

Result :


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.

PHP Topics

Ask Questions

Ask Question