How to add custom global variable declarations in Drupal settings.php

| | 1 min read

We start thinking about global variables when we want something to be available just everywhere. One case is when we want a particular variable to be available to all modules in a site. In Drupal, global variables are usually defined in settings.php. With that, all modules can access that variable.

We can use the $conf array to set those variables in settings.php and the variable_get() function to access those variables from different modules.

For example, once you have added the following in settings.php,

$conf += array(
'MY_TEST_URL' => '',
'MY_TEST_USERNAME' => 'myuname',
'MY_TEST_PASSWORD' => 'mypassword'

you can use that variable in our modules with the variable_get function as follows:

$my_url_variable = variable_get('MY_TEST_URL');