4.6.x bootstrap.inc variable_get($name, $default)
4.7.x bootstrap.inc variable_get($name, $default)
5.x bootstrap.inc variable_get($name, $default)
6.x bootstrap.inc variable_get($name, $default)
7.x bootstrap.inc variable_get($name, $default = NULL)

Returns a persistent variable.

Case-sensitivity of the variable_* functions depends on the database collation used. To avoid problems, always use lower case for persistent variable names.


$name: The name of the variable to return.

$default: The default value to use if this variable has never been set.

Return value

The value of the variable. Unserialization is taken care of as necessary.

See also



643 calls to variable_get()
ActionLoopTestCase::triggerActions in modules/simpletest/tests/actions.test
Create an infinite loop by causing a watchdog message to be set, which causes the actions to be triggered again, up to actions_max_stack times.
actions_do in includes/actions.inc
Performs a given list of actions by executing their callback functions.
AggregatorCronTestCase::testCron in modules/aggregator/aggregator.test
Adds feeds and updates them via cron process.
AggregatorTestCase::getDefaultFeedItemCount in modules/aggregator/aggregator.test
Returns the count of the randomly created feed array.
aggregator_admin_form in modules/aggregator/aggregator.admin.inc
Form constructor for the aggregator system settings.

... See full list

1 string reference to 'variable_get'
system_test_menu in modules/simpletest/tests/system_test.module
Implements hook_menu().


includes/bootstrap.inc, line 1220
Functions that need to be loaded on every Drupal request.


function variable_get($name, $default = NULL) {
  global $conf;

  return isset($conf[$name]) ? $conf[$name] : $default;


Danny_Joris’s picture

If you use variable_get() for a #default_value in a form, it is common practice to use the same variable name as the form element it's associated with.

When you do this when using system_settings_form($form) to return your admin forms, this will automatically save the new values to the variable that is given in variable_get().

  $form['form']['my_form_element'] = array(
    '#title' => t('Title'),
    '#type' => 'textarea',
    '#default_value' => variable_get('my_form_element'),
  return system_settings_form($form); 

An approach for multiple variables in one form can be found here: http://api.drupal.org/api/drupal/includes--bootstrap.inc/function/variab...

dpintats’s picture

Matching variable name to form element for system settings form seems required, not just out of practice. Could not get the variable to save unless they were the same.

gokulnk’s picture

Variable saves with the name provided in the form key. So in fact the variable is saved.

But you are doing variable_get with a different name and hence it is not present in the database and falls back to the default value.

afireintheattic’s picture

It seems that this is definitely now a requirement, as I was unable to get items to save using system_settings_form until I used the same name for both the form element and it's corresponding var.

martin_q’s picture

...but if you write your own submit handler then you can call the form elements whatever you like.

switch13’s picture

What will it do if the second parameter is simply set to FALSE?

kingandy’s picture

Same as if you set it to anything else: If the variable isn't currently set, the function will return FALSE. If it is, you'll get whatever it's been set to.

jwilson3’s picture

tamerzg’s picture

variable_get() and variable_set() are depracated in Drupal 8.
Instead use Configuration API, more info here: http://zoubi.me/blog/variable-get-and-variable-set-drupal-8