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.

Parameters

$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.

See also

variable_del(), variable_set()

298 calls to variable_get()
actions_do in includes/actions.inc
Perform a given list of actions by executing their callback functions.
aggregator_admin_settings in modules/aggregator/aggregator.admin.inc
Form builder; Configure the aggregator system.
aggregator_categorize_items in modules/aggregator/aggregator.pages.inc
Form builder; build the page list form.
aggregator_filter_xss in modules/aggregator/aggregator.module
Safely render HTML content, as allowed.
aggregator_page_categories in modules/aggregator/aggregator.pages.inc
Menu callback; displays all the categories used by the aggregator.

... See full list

File

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

Code

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

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

Comments

madmanmax’s picture

Let's take the following example:

How NOT to do it:

function my_module_settings() {
  $form['#tree'] = TRUE;	// Prevent flattening the form values
	$form['some_fields'] = array(
		'#title' => t('Some fields'),
		'#type'  => 'fieldset',
		'#collapsible' => FALSE,
	);
       $form['some_fields'] ['first_field'] = array(
		'#title'  => t('First field'),
		'#type'  => 'textfield',
                '#default_value' => variable_get('first_field','1st default value')
       );
       $form['some_fields'] ['second_field'] = array(
		'#title'  => t('Second field'),
		'#type' => 'textfield',
                '#default_value' => variable_get('second_field','2nd default value')
       );
return system_settings_form($form);
}

You might be tempted to do it like that, but if you look in the variables table you will see that there is no variable first_field or second_field, instead we find this variable: some_fields which is a serialized array. This is actually a good way to store multiple variables, because instead of having multiple SQL requests for each variable you have only one.

The solution:

function my_module_settings() {
  $form['#tree'] = TRUE;	// Prevent flattening the form values
	$form['some_fields'] = array(
		'#title' => t('Some fields'),
		'#type'  => 'fieldset',
		'#collapsible' => FALSE,
	);
       // We get the variable some_fields which contains an array with
       // the variables we are looking for (first_field and second_field)
       $some_fields = variable_get('some_fields', array());
       $form['some_fields'] ['first_field'] = array(
		'#title'  => t('First field'),
		'#type'  => 'textfield',
                '#default_value' => (!empty($some_fields['first_field']))
                    ? $some_fields['first_field']
                    : '1st default value',
       );
       $form['some_fields'] ['second_field'] = array(
		'#title'  => t('Second field'),
		'#type' => 'textfield',
                '#default_value' => (!empty($some_fields['second_field']))
                    ? $some_fields['second_field']
                    : '2nd default value', 
       );
return system_settings_form($form);
}

Hope this helps 'cause it took me a while to figure it out.

tea.time’s picture

I just ran into this exact case - wanted to use a fieldset + system_settings_form() + variable_get()s... was just looking at the variables table and thinking maybe I couldn't get the values out one by one - and then saw your post.

I ran into one further issue though. You're using empty() to check whether to use the default value, but what if you want to allow the value to be one that would return true in empty()? (i.e. I want to allow 0 as a value for one field, but 0 is considered an empty value.)

First I considered using isset() on the array returned by variable_get(), but found that even if a field is left empty, its key will still exist in the returned array.

I also wanted to organize the usage of defaults slightly differently than you did. So I ended up merging an array of defaults with the returned array after passing it through a custom filter. Say we have one field expecting a string and a second field expecting a number:

$some_fields = array_merge(
  array(
    'first_field' => 'text default',
    'second_field' => 3
  ),
  array_filter( variable_get('some_fields', array()), create_function('$value',
      'return ( is_numeric($value) && (int) $value === 0) || !empty($value);'
    )
  )
);

Then I just use the values in $some_fields to set the #default_value of all the relevant fields.

Note: with at least PHP version 5.3.0, you can use anonymous functions rather than create_function (but unfortunately I'm currently working with 5.2.10).

BassistJimmyJam’s picture

Instead of having such a complicated condition, you could use strlen() to determine whether or not the variable is empty. For example:

array_filter( variable_get('some_fields', array()), create_function('$value',
      'return strlen($value);'
    )

Would return a 0 (false) if $value is empty and a positive number (true) if $value is not empty (including "0"). If you're dead-set on it returning a boolean, you could always cast the return value:

array_filter( variable_get('some_fields', array()), create_function('$value',
      'return (Boolean) strlen($value);'
    )
Stephanie_42’s picture

Thanks! This was a big help and saved me a bunch of time!