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?
In case you need to access your variable $name within a function, you need to say "global $name;" at the beginning of that function. You need to repeat this for each function in the same file. In other words: write global $var before you use it in another function.
$GLOBALS is a PHP super global variable which is used to access global variables from anywhere in the PHP script (also from within functions or methods). PHP stores all global variables in an array called $GLOBALS[index].
Global variables and function names have external linkage. These are accessed from other files by declaring them with the keyword extern.
You have to define the variable as global first:
global $server_hostname;
$server_hostname = "localhost";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With