$a = 1; /* global scope */
echo $a; /* reference to local scope variable */
This script will not produce any output because the echo statement refers to a local version of the $a variable, and it has not been assigned a value within this scope. You may notice that this is a little bit different from the C language in that global variables in C are automatically available to functions unless specifically overridden by a local definition. This can cause some problems in that people may inadvertently change a global variable. In PHP global variables must be declared global inside a function if they are going to be used in that function.
$a = 1;
$b = 2;
global $a, $b;
$b = $a + $b;
The above script will output 3. By declaring $a and $b global within the function, all references to either variable will refer to the global version. There is no limit to the number of global variables that can be manipulated by a function.
A second way to access variables from the global scope is to use the special PHP-defined $GLOBALS array. The previous example can be rewritten as:
Example #2 Using $GLOBALS instead of global
$a = 1;
$b = 2;
$GLOBALS['b'] = $GLOBALS['a'] + $GLOBALS['b'];
The $GLOBALS array is an associative array with the name of the global variable being the key and the contents of that variable being the value of the array element. Notice how $GLOBALS exists in any scope, this is because $GLOBALS is a superglobal. Here's an example demonstrating the power of superglobals:
Example demonstrating superglobals and scope
// Most predefined variables aren't "super" and require
// 'global' to be available to the functions local scope.
// Superglobals are available in any scope and do
// not require 'global'. Superglobals are available
// as of PHP 4.1.0, and HTTP_POST_VARS is now
// deemed deprecated.
Using global keyword outside a function is not an error. It can be used if the file is included from inside a function.
This feature is possible using the global keyword.
Skills PhpQualifications :- High School - SLV, College/University - Graphic Era Deemed Univ University,
in 2006, i Join a web development company in Bangalore and start work on "Ruby on Rail" this is my first web development framework and we worked in this framework and develop four to five project after some time , I moved to JEE/Sturst frame work,
After some time i decided that now times come and I have to go on in my own way and I started a company name is "Soarlogic&q