4.6 bootstrap.inc variable_set($name, $value)
4.7 bootstrap.inc variable_set($name, $value)
5 bootstrap.inc variable_set($name, $value)
6 bootstrap.inc variable_set($name, $value)
7 bootstrap.inc variable_set($name, $value)

Sets 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 set.

$value: The value to set. This can be any PHP data type; these functions take care of serialization as necessary.

See also



370 calls to variable_set()
AccessDeniedTestCase::testAccessDenied in modules/system/system.test
ActionLoopTestCase::testActionLoop in modules/simpletest/tests/actions.test
Set up a loop with 3 - 12 recursions, and see if it aborts properly.
AggregatorRenderingTestCase::testFeedPage in modules/aggregator/aggregator.test
Creates a feed and checks that feed's page.
aggregator_update_7001 in modules/aggregator/aggregator.install
Add aggregator teaser length to settings from old global default teaser length
AJAXFormPageCacheTestCase::setUp in modules/simpletest/tests/ajax.test
Sets up a Drupal site for running functional and integration tests.

... See full list


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


function variable_set($name, $value) {
  global $conf;

  db_merge('variable')->key(array('name' => $name))->fields(array('value' => serialize($value)))->execute();

  cache_clear_all('variables', 'cache_bootstrap');

  $conf[$name] = $value;


 * Implements hook_menu().
function mymodule_menu() {
$items = array();

$items['admin/mymodule/config-set-time'] = array(
'page callback' => 'drupal_get_form',
'page arguments' => array('_mymodule_set_time_form'),
'access arguments' => array('administer node'),
'type' => MENU_CALLBACK,
'file' => 'mymodule.admin.inc',


in mymodule.admin.inc:

function _mymodule_set_time_form(&$form_state) {
$form = array();
$form['time'] = array(
'#type' => 'textfield',
'#title' => t('Time in second'),
'#default_value' => variable_get('mymodule_seconds_to_wait', 24 * 3600),    
'#required' => TRUE,
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),

_mymodule_set_time_form_submit(&$form_state) {
  if (
intval($form_state['values']['time'])) {
variable_set('mymodule_seconds_to_wait', intval($form_state['values']['time']));
drupal_set_message(t('Your configuration has been saved.'));

Thanks. Very useful.

It would also be good to look at system_settings_form() for these types of forms.

All data created by variable_set() is loaded from the database at bootstrap. Calling variable_get() doesn't perform the database request to get you the data, it simply declares the $conf global and returns the variable you've asked for. Therefore, don't carelessly use variable_set() to store lots of data that you will only use on individual pages or in custom methods. Use your own little table and storage functions for that.

haven't tested passing other functions as $value, but very surprised that $var++ isn't evaluated before being passed.

variable_set('var', $var++);   $var=$get_returned=variable_get('var','default_var_not_being_set');
//$var is 0

The postfix-plusplus-operator increments your variable, after assignement. See the PHP Documentation for more information.

$var++); // prints 0

$var); // prints 1

It would be cool to have support for passing arrays into this function to set more than one variable at a time in future drupal releases.

'mimemail_sitestyle' => 0,
'mimemail_linkonly' => 1,

How about this:

  array_map('variable_set', array_keys($a), array_values($a));

Can anybody elaborate this, Is this will work as a session, what is the actual mechanism behind this?

Have a look under the hood: each variable has its own entry in the system {variable} db table. These variables are not user or session-specific but are available at all times for all users. Session variables, like in plain-jane PHP, are used by $_SESSION superglobal with the difference that Drupal also automatically manages db-stored session variables for easier load distribution across servers.

Wouldn't it be nice if this function actually returned the $value you passed in. This way you could set local variables when you do your variable_set so you can use that value in your first pass.

I got here because I thought the very same thing as mrvanmeter. Seems like a missed opportunity.