1

i'd like to set up some global variables for storing several things. i've tried it like this:

function init_web()
{
    $webname = "myweb";
    $web['webname'] = $webname;
    $web['server_root'] = $_SERVER['DOCUMENT_ROOT']."/$webname/"; 
    $web['lang']="en";
}

the problem is that i can't access those variables inside of functions .. i've tried using global $web; but didnt help.

what's the trick to get it global?

thanks

Fuxi
  • 7,611
  • 25
  • 93
  • 139
  • i found out that it also works like this: $web = init_web(); where init_web returns the $web variable, then use global $web; but i think it's the same as suggested. – Fuxi Mar 28 '11 at 16:47

6 Answers6

5

While you'll get the usual "global variables are bad" crying, here's the basics:

$web = array(); // define the var at the "top level" of the code tree, outside any functions/classes.
function init_web() {
    global $web; // make it visible in the function
    $web['lang'] = 'en'; // make some settings
}

basically, you had it, but hadn't defined the variable outside the function. Just saying 'global' within the function won't magically create one outside the function - it already has to exist before you try to "internalize" it to the function and change/access its contents.

mario
  • 144,265
  • 20
  • 237
  • 291
Marc B
  • 356,200
  • 43
  • 426
  • 500
2

You are on the right track:

$web = array();

function init_web()
{
    global $web;
    $webname = "myweb";
    $web['webname'] = $webname;
    $web['server_root'] = $_SERVER['DOCUMENT_ROOT']."/$webname/"; 
    $web['lang']="en";
}
jeroen
  • 91,079
  • 21
  • 114
  • 132
1

You can define them constant

define('WEBNAME',"myweb");

and can use everywhere in your application because constant are global in nature by default.

and this is the way to store constant as constant as they never changed dynamically until you move to new server or change configuration.

Shakti Singh
  • 84,385
  • 21
  • 134
  • 153
1

you can use session variables:

session_start(); // at the top of the php page
function init_web()
{
    $webname = "myweb";
    $_SESSION['webname'] = $webname;
    $_SESSION['server_root'] = $_SERVER['DOCUMENT_ROOT']."/$webname/"; 
    $_SESSION['lang']="en";
}

now they can be 'globally' accessable :-)

Naftali
  • 144,921
  • 39
  • 244
  • 303
0

Declare $web outside the function and reference it inside with the $GLOBALS superglobal:

// Declare in global scope
$web = array();

function init_web()
{
  $webname = "myweb";
  // Access via superglobal in function scope
  $GLOBALS['web']['webname'] = $webname;
  $GLOBALS['web']['server_root'] = $_SERVER['DOCUMENT_ROOT']."/$webname/"; 
  $GLOBALS['web']['lang']="en";
}
Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
0

If you're just storing scalar values (strings, ints, floats - not arrays, objects), you should use define().

This will make your configurations global and constant.

As to answer your question,

First define your variables outside the scope of that function (perhaps in a config file) and then use the global keyword to make it global when you need them.

JohnP
  • 49,507
  • 13
  • 108
  • 140