PHP Global Variable

PHP Global Variable :

  1. If the variable is accessed from any part of program then it is called as global variable.
  2. We need to explicitly declare global variable using GLOBAL keyword if we need to update the variable.
  3. GLOBAL ketword will recognize the global variable.

Example : PHP Global Variable

<!DOCTYPE html>

$num = 25;

function modify() {
  GLOBAL $num;
  print "Result is : $num";



Output :

Result is : 26

Explanation :

We have declared a variable and assigned value 25.

$num = 25;

Now again we have decalred same variable in the function with preceeding GLOBAL keyword.


Placing GLOBAL keyword in front of an already existing variable will tells PHP to use the variable having that name.