Global variables are defined outside the function, starting at the definition of the variable and at the end of the program file. However, unlike other languages, PHP's global variables are not automatically set to be available, in PHP the function can be considered as a separate program fragment, local variables will override the visibility of global variables, therefore, in the function cannot directly call global variables.
As follows:
<? PHP $one=100; $two=200; // Defining global Variables funcation Demo () { echo "operation Result:". ( $one+$two). " <br> "; // The equivalent of a new declaration within the function of two function variables not assigned to the initial value, the result of the operation: 0 }demo ();? >
The correct way to refer is as follows:
<?php$one=100; $two =200; Define Global Variables Funcation demo () {global $one, $two; echo "Operation Result:". ( $one + $two). " <br> "; Operation Result: 300, using the global variable declared outside the function}demo (); >
Referencing the $global array of super-global variables
<?php$one=100; $two =200; Define Global Variables Funcation demo () { echo ' operation result: ". ( $GLOBAL [' One ']+ $GLOBAL [' $two ']). " <br> "; Operation Result: 300, using the global variable declared outside the function}demo (); >
Global variable references in PHP