4.6.x bootstrap.inc variable_set($name, $value)
4.7.x bootstrap.inc variable_set($name, $value)
5.x bootstrap.inc variable_set($name, $value)
6.x bootstrap.inc variable_set($name, $value)
7.x 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



376 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 1242
Functions that need to be loaded on every Drupal request.


function variable_set($name, $value) {
  global $conf;
    'name' => $name,
    'value' => serialize($value),
  cache_clear_all('variables', 'cache_bootstrap');
  $conf[$name] = $value;


trungonly’s picture

 * 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',

  return $items;

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'),

function _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.'));
fenda’s picture

Thanks. Very useful.

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

eelkeblok’s picture

system_settings_form() takes some of that work away from you, specifically saving keys from the form as variables in the database. No need to call variable_set() yourself, in most cases.

vanphat_92’s picture

Sir,i wonder where is "mymodule_seconds_to_wait"

charlie-s’s picture

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.

jaypark’s picture

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
znerol’s picture

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

print($var++); // prints 0

print(++$var); // prints 1
splatterb0y’s picture

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,
warbo’s picture

How about this:

  array_map('variable_set', array_keys($a), array_values($a));
rintoug’s picture

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

texas-bronius’s picture

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.

jwilson3’s picture

mrvanmeter’s picture

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.

nazarioa’s picture

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

eelkeblok’s picture

Maybe too obvious to actually mention, but a simple workaround would be to assign your value to a (PHP) variable first, and then pass that into variable_set(). Also, I think there might be a performance benefit to not pass the result back (especially considering it is not extremely common to need to use the value in the subsequent code); AFAICT, PHP will do a pass by value back from the function, which could be heavy in case of large arrays (which you maybe should not be putting into variable_set(), but what you shouldn't do and what people actually do are distinctly different things).

Tushar Rana’s picture

I have implemented in my custom setting form and in that form submit i have used variable_set to save my settings but i can't find in the database anywhere, just need to know is there any restriction with this function to work only with bootstrap inc
if yes then how do i add that in my custom module to achieve my goal
i am using default theme of drupal 7

variable_set($key, $value);
W.M.’s picture

Who can use this function, only administrator? Thanks.

eelkeblok’s picture

This is an API function to be called from PHP code. It does not do any permissions checking, that would be handled somewhere before this is actually called, if applicable.

aaronbauman’s picture

In Drupal 8, variables are decomposed into "state" and "config".

Typically, "config" settings are exportable values which can be re-used in modules, features, or installation profiles.
And "state" settings are values which should not be exported to code, and only make sense in the context of one site.
For example, cron key is a "state" variable; the front page path is a "config" variable.

To set a config value:

  ->set('page.front', 'node/1')

see also: Updating variables to D8 config system

To set a state value:

  ->set('system.cron_key', md5("This is not a good cron key")); 

see also: Updating variables to D8 State system