-2

How do I get the value of a persistent variable with variable_get()? Does the function save the value on a database table or somewhere else?

1

To get the value use:

$valueVar = variable_get('myvar');

It is saved on a generic database table called [prefix]_variable, if you are not using prefix, the name of the table is variable. Look at: https://www.drupal.org/forum/support/module-development-and-code-questions/2009-05-14/help-which-table-is-the-variable

| improve this answer | |
0

You can use variable_set() to set the value and that will store it in the database. e.g.

variable_set('myvar',123);

Then to retreive it:

$myvar = variable_get('myvar');

You could create a page with a form to allow CMS users to change the values of your variables. See this for examples: https://api.drupal.org/api/drupal/modules%21system%21system.module/function/system_settings_form/7.x

| improve this answer | |
0

variable_get() only retrieves the value of a persistent variable; it doesn't save its value as that is the task for variable_set().

Actually, variable_get() gets the persistent variable value from the global $conf which is initialized from the following code in _drupal_bootstrap_variables().

  global $conf;

  // Initialize the lock system.
  require_once DRUPAL_ROOT . '/' . variable_get('lock_inc', 'includes/lock.inc');
  lock_initialize();

  // Load variables from the database, but do not overwrite variables set in settings.php.
  $conf = variable_initialize(isset($conf) ? $conf : array());

The code in variable_initialize() first tries to load the persistent variable values from the cache, and then load the values from the database (the variable database table).

  if ($cached = cache_get('variables', 'cache_bootstrap')) {
    $variables = $cached->data;
  }
  else {
    // Cache miss. Avoid a stampede.
    $name = 'variable_init';
    if (!lock_acquire($name, 1)) {

      // Another request is building the variable cache.
      // Wait, then re-run this function.
      lock_wait($name);
      return variable_initialize($conf);
    }
    else {
      // Proceed with variable rebuild.
      $variables = array_map('unserialize', db_query('SELECT name, value FROM {variable}')
        ->fetchAllKeyed());
      cache_set('variables', $variables, 'cache_bootstrap');
      lock_release($name);
    }
  }
  foreach ($conf as $name => $value) {
    $variables[$name] = $value;
  }
  return $variables;

To answer the values of persistent variables can be stored:

  • In the global $conf initialized in the settings.php file (which means in memory)
  • In the cache, which isn't necessarily stored in the database, especially if there are third-party modules that change the cache functions
  • In the variable database table, when saved using variable_set()
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.