deltanovember
deltanovember

Reputation: 44051

Why doesn't my code work when I try to access global variables in my PHP function?

I have

require_once 'db_connect.php'

inside which is

$db_name = ...

Then directly below I have

function my_func() {
global $db_name; 
}

However $db_name is returning a blank value. What am I doing wrong?

Update: If I print $db_name directly after require_once it works fine. However all this is part of a sidebar in wordpress therefore I'm not sure about function scope.

Upvotes: 0

Views: 445

Answers (3)

Andrew Moore
Andrew Moore

Reputation: 95364

EDIT: My answer was tailored to the original version of the question. Please note that the answer below was an answer based on information available at the time.

When you require() or include() the included script runs in the same scope than the line where the file is being included.

Consider the following:

myscript.php

<?php
function init_db() {
  require_once("database.inc.php");
}

echo $db_name;

function test() {
  global $db_name;
  echo $db_name;
}

test();

database.inc.php

<?php
$db_name = "hello";

Since database.inc.php is being included in the function init_db(), none of the echo will actually output anything. $db_name is local to the function init_db().

To remedy that, simply add global $db_name; to the database.inc.php file:

database.inc.php

<?php
global $db_name;
$db_name = "hello";

Think of require() or include() as copy-pasting code where it is called. Since you are inside the function init_db() when database.inc.php is executed, global $db_name; will allow you to set $db_name globally.

Upvotes: 3

German Rumm
German Rumm

Reputation: 5832

Although you can use global keyword to bring variables into the scope, I would recommend using superglobal $GLOBALS array. (Explicit is better than implicit). And I mean not only in your function that uses a global variable, but also in a file that defines it.

// db_connect.php
$GLOBALS['db_name'] = 'my_db_name';

This way, when someone (even you, a month later) is looking at your db_connect.php he will instantly understand that this variable is supposed to be in global scope, and will think twice before trying to rename it.

Upvotes: 0

GordonM
GordonM

Reputation: 31740

The two things that spring to mind are, is $db_name actually being assigned the value you expect it to be getting, and are you including the file in the global scope?

If $db_name is being assigned a value by a function that might fail (for example mysql_connect will only return a connection handle if a connection with the database is established) then $db_name might be being assigned a NULL value.

Included files inherit the scope from which the include statement was executed. If you include inside a function, then any values assigned in the include will only be in that function's scope and will cease to exist outside of scope. Ditto for classes and methods. Please make sure your include statement is being executed in the global scope.

Upvotes: 0

Related Questions