[php] Good things to know about global variable in PHP
From 4.1.0 (or 4.2.0)
Default register_globals in php.ini is off
This disables automatic forms input name to PHP vars function
(Eg.<input name=simpletext > => in php the input value is available as $simpletext with register_globals on )
Therefore, you need to access below Globals to access them
Also, short_open_tags is off
This disables <? ?> php tag. so you need to use <?php ?>.
// Available since PHP 4.1.0
print $_POST['username'];
print $_GET['username'];
print $_COOKIE['username'];
print $_REQUEST['username']; // contains all get post cookie vars
print $_FILES[]' // upload file related...
import_request_variables('p', 'p_'); //p : post g: get c: cookie ... p_ : prefix
print $p_username;
// Available since PHP 3.
print $HTTP_POST_VARS['username'];
// $HTTP_GET_VARS
// $HTTP_COOKIE_VARS
// $HTTP_POST_FILES
// Available if the PHP directive register_globals = on. As of
// PHP 4.2.0 the default value of register_globals = off.
// Using/relying on this method is not preferred.
print $username;
?>
Global variables: $GLOBALS
An associative array containing references to all variables which are currently
defined in the global scope of the script. The variable names are the keys of the array.
$_SERVER['PHP_SELF'] //current script name
$_SERVER['SERVER_NAME'] // web site host address
‘QUERY_STRING’
‘DOCUMENT_ROOT’ // system document root
‘SCRIPT_FILENAME’ //The absolute pathname of the currently executing script
If you want to access global variables in function , use extract($GLOBALS);
function test(){
extract($GLOBALS);
// you can access all global variables including the ones you have defined
}
Or you can just define the global variables that you need
function test(){
global $testvalue;
}
