PHP Global Variable
To access global variable in PHP we need to use global keyword or $GLOBALS associative array
global is a keyword which helps us to access the globally declared variable. There is no limit to use global keyword within a program.
$GLOBALS is an associative array containing references to all global variables, the variable names are the keys of the array.
Code for PHP Global Variable:
<?php $a=1; function globe()
{ $a="12";
echo "Value of a is:".$a;
echo "<br />";
global $a;//global keyword helps us to access globally declared variables echo "Value of a is :".$a;
echo "<br> Other way to access global variable";
echo " <Br>Value of a is :".$GLOBALS["a"];//using $GLOBALS associative array. } globe(); ?>
Output:
Value of a is:12
Value of a is :1
Other way to access global variable
Value of a is :1
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.