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) |
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
354 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
- AJAXFrameworkTestCase::testLazyLoadOverriddenCSS in modules/
simpletest/ tests/ ajax.test - Tests that overridden CSS files are not added during lazy load.
File
- includes/
bootstrap.inc, line 968 - Functions that need to be loaded on every Drupal request.
Code
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;
}
Comments
Code to set a variable in admin panel using form field
<?php
$items['admin/mymodule/config-set-time'] = array(
/*
* Implements hook_menu().
*/
function mymodule_menu() {
$items = 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
:<?php
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.'));
}
}
?>
Thanks. Very useful.
Thanks. Very useful.
It would also be good to look at system_settings_form() for these types of forms.
Not a generic storage function.
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.
cannot pass $value as an incremented value
haven't tested passing other functions as $value, but very surprised that $var++ isn't evaluated before being passed.
$var=0;
variable_set('var', $var++); $var=$get_returned=variable_get('var','default_var_not_being_set');
//$var is 0
The postfix-plusplus-operator
The postfix-plusplus-operator increments your variable, after assignement. See the PHP Documentation for more information.
<?php
$var=0;
print($var++); // prints 0 $var=0;
print(++$var); // prints 1
?>
Batch-Operation
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.
<?php
variable_set(array(
'mimemail_sitestyle' => 0,
'mimemail_linkonly' => 1,
));
?>
Is that work as session ?
Can anybody elaborate this, Is this will work as a session, what is the actual mechanism behind this?
In Drupal 8 use config() instead of variable_set()
variable_get/set is deprecated from Drupal 8.
This handbook page explains how to upgrade to the new configurations system.