function variable_set

You are here

7 bootstrap.inc variable_set($name, $value)
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)

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.

Parameters

$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()

72 calls to variable_set()
book_block in modules/book/book.module
Implementation of hook_block().
book_node_type in modules/book/book.module
Implementation of hook_node_type().
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.

... See full list

File

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

Code

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;
}

Comments

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.

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);
}

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:

variable_set_start(array_of_variables_and_values);
variable_set_commit();

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

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

<?php
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.

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

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:

<?php
//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...
variable_bulk_set($my_vars);
?>

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:

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

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