[php] Good things to know about global variable in PHP

[php] Good things to know about global variable in PHP
0 votes, 0.00 avg. rating (0% score)

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 ?>.




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;
}

Leave a Reply

Your email address will not be published. Required fields are marked *