6

I'm trying to import some variables from a PHP script. It seems simple but I cannot get it to work.

The script contains some global variables like that:

$server_hostname = "localhost";
$server_database = "kimai";
$server_username = "root";
$server_password = "";
$server_conn     = "mysql";
$server_type     = "";
$server_prefix   = "kimai_";
$language        = "en";
$password_salt   = "7c0wFhYHHnK5hJsNI9Coo";

Then in my script, I would like to access these variables, so I've done:

require_once 'includes/autoconf.php';   
var_dump($server_hostname);

But this just outputs NULL. I've also tried:

require_once 'includes/autoconf.php';

global $server_hostname;    
var_dump($server_hostname);

but still not working.

I've added some echo statements in the "autoconf.php" file so I know that it's being loaded.

Any idea how I could access these variables?

6
  • If they are defined in a function then it will be private to that function. Even if you use global elsewhere Commented May 18, 2012 at 6:56
  • They are not in a function. The code I posted is the complete code. Commented May 18, 2012 at 6:58
  • do you have any other errors except that the variable does not seem to be in the namespace? activate the error output if the errors are hidden. Commented May 18, 2012 at 6:59
  • Check out PHP.ini file for global variable settings, im not sure if there is a setting but could be worth a look Commented May 18, 2012 at 7:09
  • have you tried using include_once("includes/autoconf.php"); ? Commented May 18, 2012 at 7:10

7 Answers 7

3

You have to define the variable as global first:

global $server_hostname;
$server_hostname = "localhost";
Sign up to request clarification or add additional context in comments.

3 Comments

But anyway, I don't think that this is needed if you later on do a inlude or require, it should work just as you tried.
My understanding is that variables declared outside of any function or block are already in the global scope (php.net/manual/en/language.variables.scope.php) Also I cannot modify this script directly so I need to access the variables some other way.
They are in the global scope but in other files or functions you need to add global $varname;
2

It turns out the file was included somewhere else in the application so when I was calling require_once, the file was not being included at all. I changed it to just require and now it works.

Comments

1

Maybe the file was not included properly.

require_once 'includes/autoconf.php';   

check current work directory where you include autoconf.php

try this

if (file_exists('includes/autoconf.php')) require_once 'includes/autoconf.php';
else echo 'File not exists';

to check it out.

Comments

0

How about using a constant?

define("server_hostname","localhost"); define("server_hostname","localhost");

3 Comments

I cannot change the code in this autoconf.php file. Of course, if I could I'd simply avoid using any global variable at all.
TRUE but isn't this a constant, not variable
that is semantic anyway. The fact that a variable is not loaded, it means that the constant will not be loaded as well. Is a loading issue .
0

If you include the file and the variable is in a plain text, and not inside a function / class it works without the global

Go to your php.ini and put display_errors=On and errors to E_ALL so you will see which is the correct reason

Comments

0

better way to use and correct global variables is first assign value to variable and then declare global. this is:

$server_hostname = "localhost";
global $server_hostname;

Comments

0

This is evil, but it may get the job done.

<? //PHP 5.4+
\call_user_func(static function(){
    $globals = \get_defined_vars();
    include 'includes/autoconf.php';
    $newVars = \array_diff_key($globals, \get_defined_vars());
    foreach($newVars as $name => $value){
        \define($name, $value);
    }
});
//Variables defined in file are now constants!
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.