downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | conferences | my php.net

search for in the

$_SERVER> <Süper küreseller
[edit] Last updated: Fri, 07 Jun 2013

view this page in

$GLOBALS

$GLOBALSKüresel alanda bulunan bütün değişkenleri içerir

Açıklama

Geçerli betiğin küresel alanında tanımlı bütün değişkenleri içeren bütünleşik bir dizidir. Değişken isimleri dizinin anahtarlarıdır.

Örnekler

Örnek 1 - $GLOBALS örneği

<?php
function test() {
    
$foo "yerel değişken";

    echo 
'$foo küresel alanda: ' $GLOBALS["foo"] . "\n";
    echo 
'$foo yerel alanda: ' $foo "\n";
}

$foo "Örnek içerik";
test();
?>

Yukarıdaki örnek şuna benzer bir çıktı üretir:

$foo küresel alanda: Örnek içerik
$foo yerel alanda: yerel değişken

Notlar

Bilginize:

Bu bir süper küreseldir. Yani bir betiğin her yerinde geçerlidir. Değişkene işlevler ve yöntemlerin içinden erişmek için global $değişken; deyimine gerek yoktur.

Bilginize: Değişken kullanılabilirliği

Diğer süper küresellerden farklı olarak, $GLOBALS PHP'de her zaman var olmuştur.



$_SERVER> <Süper küreseller
[edit] Last updated: Fri, 07 Jun 2013
 
add a note add a note User Contributed Notes $GLOBALS - [6 notes]
up
1
mstraczkowski at gmail dot com
8 days ago
Watch out when you are trying to set $GLOBALS to the local variable.

Even without reference operator "&" your variable seems to be referenced to the $GLOBALS

You can test this behaviour using below code

<?php
/**
 * Result:
 * POST: B, Variable: C
 * GLOBALS: C, Variable: C
 */
 
// Testing $_POST
$_POST['A'] = 'B';
 
$nonReferencedPostVar = $_POST;
$nonReferencedPostVar['A'] = 'C';
 
echo
'POST: '.$_POST['A'].', Variable: '.$nonReferencedPostVar['A']."\n\n";
 
// Testing Globals
$GLOBALS['A'] = 'B';
 
$nonReferencedGlobalsVar = $GLOBALS;
$nonReferencedGlobalsVar['A'] = 'C';
 
echo
'GLOBALS: '.$GLOBALS['A'].', Variable: '.$nonReferencedGlobalsVar['A']."\n\n";
up
4
therandshow at gmail dot com
1 year ago
As of PHP 5.4 $GLOBALS is now initialized just-in-time. This means there now is an advantage to not use the $GLOBALS variable as you can avoid the overhead of initializing it. How much of an advantage that is I'm not sure, but I've never liked $GLOBALS much anyways.
up
1
bkilinc at deyta dot net
2 months ago
I prefer accessing globals through static function calls. Source code looks better; I use glb::get('myglobalvar') instead of $GLOBALS['myglobalvar']. This gives me full control over global access, which can be the source of problems in practice.

class glb
{
    static public function set($name, $value)
    {
        $GLOBALS[$name] = $value;
    }

    static public function get($name)
    {
        return $GLOBALS[$name];
    }

}

$myglobalvar = 'Hello, World !';

function myfunction()
{
    $val = glb::get('myglobalvar');
    echo "$val\n";
    glb::set('myglobalvar', 'hi, again :)');
    $val = glb::get('myglobalvar');
    echo "$val\n";
}

myfunction();
up
-1
ravenswd at yahoo dot com
4 years ago
Keep in mind that $GLOBALS is, itself, a global variable. So code like this won't work:

<?php
   
print '$GLOBALS = ' . var_export($GLOBALS, true) . "\n";
?>

This results in the error message: "Nesting level too deep - recursive dependency?"
up
-2
David
4 years ago
Though you can use var_dump to output the value of $GLOBALS.
up
-5
Gratcy
1 year ago
this is technique that i always did for configuration file..

<?php
$conf
['conf']['foo'] = 'this is foo';
$conf['conf']['bar'] = 'this is bar';

function
foobar() {
    global
$conf;
   
var_dump($conf);
}

foobar();

/*
result is..

array
  'conf' =>
    array
      'foo' => string 'this is foo' (length=11)
      'bar' => string 'this is bar' (length=11)

*/
?>

 
show source | credits | sitemap | contact | advertising | mirror sites