Community Documentation

variable_set

5 bootstrap.inc variable_set($name, $value)
6 bootstrap.inc variable_set($name, $value)
7 bootstrap.inc variable_set($name, $value)
8 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 functions call 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.
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.
default_profile_tasks in profiles/default/default.profile
Perform any final installation tasks for this profile.
drupal_clear_js_cache in includes/common.inc
Delete all cached JS files.
drupal_cron_run in includes/common.inc
Executes a cron run when called
drupal_get_private_key in includes/common.inc
Ensure the private key variable used to generate tokens is set.
drupal_http_request in includes/common.inc
Perform an HTTP request.
example_form_submit in developer/example.profile
Form API submit for the example form.
example_profile_tasks in developer/example.profile
Perform any final installation tasks for this profile.
file_directory_temp in includes/file.inc
Determine the default temporary directory.
filter_admin_overview_submit in modules/filter/filter.admin.inc
forum_block in modules/forum/forum.module
Implementation of hook_block().
forum_enable in modules/forum/forum.install
forum_form_submit in modules/forum/forum.admin.inc
Process forum form and container form submissions.
forum_taxonomy in modules/forum/forum.module
Implementation of hook_taxonomy().
forum_update_6000 in modules/forum/forum.install
Create the forum vocabulary if does not exist. Assign the vocabulary a low weight so it will appear first in forum topic create and edit forms. Do not just call forum_enable() because in future versions it might do something different.
hook_block in developer/hooks/core.php
Declare a block or set of blocks.
hook_node_type in developer/hooks/node.php
Act on node type changes.
hook_taxonomy in developer/hooks/core.php
Act on taxonomy changes.
hook_update_index in developer/hooks/core.php
Update Drupal's full-text index for this module.
install_configure_form_submit in ./install.php
Form API submit for the site configuration form.
install_main in ./install.php
The Drupal installation happens in a series of steps. We begin by verifying that the current environment meets our minimum requirements. We then go on to verify that settings.php is properly configured. From there we connect to the configured database…
install_tasks in ./install.php
Tasks performed after the database is initialized.
locale_add_language in includes/locale.inc
API function to add a language.
locale_languages_configure_form_submit in includes/locale.inc
Submit function for language negotiation settings.
locale_languages_edit_form_submit in includes/locale.inc
Process the language editing form submission.
locale_languages_overview_form_submit in includes/locale.inc
Process language overview form submissions, updating existing languages.
locale_update_6000 in modules/locale/locale.install
{locales_meta} table became {languages}.
locale_update_6005 in modules/locale/locale.install
Change language setting variable of content types.
locale_update_js_files in modules/locale/locale.module
Update JavaScript translation file, if required, and add it to the page.
menu_rebuild in includes/menu.inc
(Re)populate the database tables used by various menu functions.
node_access_needs_rebuild in modules/node/node.module
Flag / unflag the node access grants for rebuilding, or read the current value of the flag.
node_type_form_submit in modules/node/content_types.inc
Implementation of hook_form_submit().
node_update_index in modules/node/node.module
Implementation of hook_update_index().
profile_block in modules/profile/profile.module
Implementation of hook_block().
statistics_block in modules/statistics/statistics.module
Implementation of hook_block().
statistics_cron in modules/statistics/statistics.module
Implementation of hook_cron().
system_block in modules/system/system.module
Implementation of hook_block().
system_check_http_request in modules/system/system.module
Checks whether the server is capable of issuing HTTP requests.
system_image_toolkit_settings in modules/system/system.admin.inc
Form builder; Configure site image toolkit usage.
system_node_type in modules/system/system.module
Implementation of hook_node_type().
system_settings_form_submit in modules/system/system.module
Execute the system_settings_form.
system_themes_form_submit in modules/system/system.admin.inc
Process system_themes_form form submissions.
system_theme_settings_submit in modules/system/system.admin.inc
Process system_theme_settings form submissions.
system_update_1022 in modules/system/system.install
Add index on users created column.
system_update_6014 in modules/system/system.install
Record that the installer is done, so it is not possible to run the installer on upgraded sites.
system_update_6017 in modules/system/system.install
Rename settings related to user.module email notifications.
system_update_6021 in modules/system/system.install
Migrate the menu items from the old menu system to the new menu_links table.
system_update_6042 in modules/system/system.install
Upgrade recolored theme stylesheets to new array structure.
system_update_6043 in modules/system/system.install
Update table indices to make them more rational and useful.
system_update_6046 in modules/system/system.install
Ensure that the file_directory_path variable is set (using the old 5.x default, if necessary), so that the changed 6.x default won't break existing sites.
throttle_exit in modules/throttle/throttle.module
Implementation of hook_exit().
update_fix_d6_requirements in ./update.php
Perform Drupal 5.x to 6.x updates that are required for update.php to function properly.
update_settings_submit in modules/update/update.settings.inc
Submit handler for the settings tab.
user_block in modules/user/user.module
Implementation of hook_block().
_book_install_type_create in modules/book/book.install
_color_render_images in modules/color/color.module
Render images that match a given palette.
_drupal_flush_css_js in includes/common.inc
Helper function to change query-strings on css/js files.
_install_locale_initial_batch_finished in ./install.php
Finished callback for the first locale import batch.
_install_locale_remaining_batch_finished in ./install.php
Finished callback for the second locale import batch.
_install_profile_batch_finished in ./install.php
Finished callback for the modules install batch.
_locale_invalidate_js in includes/locale.inc
Force the JavaScript translation file(s) to be refreshed.
_locale_rebuild_js in includes/locale.inc
(Re-)Creates the JavaScript translation file for a language.
_menu_router_build in includes/menu.inc
Helper function to build the router table based on the data from hook_menu.
_menu_set_expanded_menus in includes/menu.inc
Helper function to update a list of menus with expanded items
_node_index_node in modules/node/node.module
Index a single node.
_update_refresh in modules/update/update.fetch.inc
Fetch project info via XML from a central server.

File

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

Code

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

Only use on admin pages

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.

I agree with nrambeck

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

wish: set multiple variables in one transaction?

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

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

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 solution to the problem!

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');
?>

Login or register to post comments