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)
*/
?>
$GLOBALS
(PHP 4, PHP 5)
$GLOBALS — Hace referencia a todas las variables disponibles en el ámbito global
Descripción
Es un array asociativo que contiene las referencias a todas la variables que están definidas en el ámbito global del script. Los nombres de las variables son las claves del array.
Ejemplos
Ejemplo #1 Ejemplo de $GLOBALS
<?php
function test() {
$foo = "variable local";
echo '$foo en el ámbito global: ' . $GLOBALS["foo"] . "\n";
echo '$foo en el ámbito simple: ' . $foo . "\n";
}
$foo = "Contenido de ejemplo";
test();
?>
El resultado del ejemplo sería algo similar a:
$foo en el ámbito global: Contenido de ejemplo $foo en el ámbito simple: variable local
Notas
Nota:
Esta es una 'superglobal' o una variable automatic global. Significa simplemente que es una variable que está disponible en cualquier parte del script. No hace falta hacer global $variable; para acceder a la misma desde funciones o métodos.
Nota: Disponibilidad de las variables
A diferencia de todas las otras superglobals, $GLOBALS ha estado esencialmente siempre disponible en PHP.
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.
Better yet, use print_r. While var_dump does detect the recursion that var_export fails on, it seems to recurse one level first for my setup. So var_dump ends up printing all globals twice, but print_r prints them only once since it detects the recursion right away. Serialize seems to not detect the recursion at all either, similar to var_export.
Though you can use var_dump to output the value of $GLOBALS.
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?"
