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

variable_del(), variable_get()

53 calls to variable_set()
cache_clear_all in includes/cache.inc
Expire data from the cache. If called without arguments, expirable entries will be cleared from the cache_page and cache_block tables.
cache_get in includes/cache.inc
Return data from the persistent cache. Data may be stored as either plain text or as serialized data. cache_get will automatically return unserialized objects and arrays.
color_scheme_form_submit in modules/color/color.module
Submit handler for color change form.
comment_update_1 in modules/comment/comment.install
Changed node_comment_statistics to use node->changed to avoid future timestamps.
comment_update_6002 in modules/comment/comment.install
Changed comment settings from global to per-node -- copy global settings to all node types.

... See full list


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


function variable_set($name, $value) {
  global $conf;
  $serialized_value = serialize($value);
  db_query("UPDATE {variable} SET value = '%s' WHERE name = '%s'", $serialized_value, $name);
  if (!db_affected_rows()) {
    @db_query("INSERT INTO {variable} (name, value) VALUES ('%s', '%s')", $name, $serialized_value);
  cache_clear_all('variables', 'cache');
  $conf[$name] = $value;


nrambeck’s picture

Take care to only use drupal_set() on admin pages. If drupal_set() is executed during regular user browsing, it constantly invalidates the variables cache, making it useless and putting extra load on your database. This load can be quite significant the larger your site is.

netgenius.co.uk’s picture

Looking at the code, I was just thinking the same as nrambeck - and the same issue exists with variable_del. This means variable_set is not a good way of storing some value that you need to update frequently. Using cache_set instead may be a good alternative (or use your own database table, but that's more complicated of course.) Queryable Variables might be a better option.

Furthermore, since this behaviour of variable_set is a potential cause of significant slow-down, it may be worth checking that nothing is calling variable_set or variable_del on a frequent or per_page basis. i.e. edit bootstrap.inc and add something like the following into variable_set ...

// If we're not posting a form, we're not expecting variable_set, so log it.
if (empty($_POST)) {
  watchdog('variable_set', $name);
apotek’s picture

I *hate* how Drupal forces me to clear the whole cached variable table for every variable I need to set. There should be a means to set several values at a time, in a single transaction. An example:


It would only do a cache clear at the end of the transaction, rather than once for every variable I'm setting.

Raf’s picture

Here you go. $vars is an associative array. The keys are the names of the vars you're setting, the values're the values.

function variable_bulk_set($vars) {
  global $conf;

  foreach($vars as $name => $value) {
    $serialized_value = serialize($value);
    db_query("UPDATE {variable} SET value = '%s' WHERE name = '%s'", $serialized_value, $name);
    if (!db_affected_rows()) {
      @db_query("INSERT INTO {variable} (name, value) VALUES ('%s', '%s')", $name, $serialized_value);
    $conf[$name] = $value;

  cache_clear_all('variables', 'cache');

That was so simple I'm actually starting to wonder if there're any pitfalls I fell into.

Berdir’s picture

There is an issue for that which didn't made it into D7. You're welcome to chime in: http://drupal.org/node/317930

drupalfever’s picture

I like your function but I think that some extra explanation wouldn't hurt.

To use Raf's function, you need to do the following:

//Set an array to hold your variables...
$my_vars = array();

//Add your variables to the array...
$my_vars['first_var'] = 'first_value';
$my_vars['second_var'] = 'second_value';
$my_vars['third_var'] = 'third_value';

//Call Raf's function to store your array of variables...

To retrieve your variables just use the "variable_get()" function normally. You will get each variable individually. The array is created only when the variables are stored.

Here is an example of how to get each variable:

//Retrieving the first variable from Drupal...
$first_var = variable_get('first_var');

//Retrieving the second variable from Drupal...
$second_var = variable_get('second_var');