Same name and namespace in other branches
- 4.6.x includes/bootstrap.inc \variable_set()
- 4.7.x includes/bootstrap.inc \variable_set()
- 5.x includes/bootstrap.inc \variable_set()
- 6.x includes/bootstrap.inc \variable_set()
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
405 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.
File
- includes/
bootstrap.inc, line 1307 - 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
in
mymodule.admin.inc:Thanks. Very useful.
Thanks. Very useful.
It would also be good to look at system_settings_form() for these types of forms.
Please use system_settings_form() for that
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.
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.
The postfix-plusplus-operator
The postfix-plusplus-operator increments your variable, after assignement. See the PHP Documentation for more information.
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.
How about this:
How about this:
Is that work as session ?
Can anybody elaborate this, Is this will work as a session, what is the actual mechanism behind this?
Drupal system variables table
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.
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.
Return value
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.
I got here because I thought
I got here because I thought the very same thing as mrvanmeter. Seems like a missed opportunity.
Maybe too obvious to actually
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).
Is this restricted only to bootstrap
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
In Drupal 8
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:
see also: Updating variables to D8 config system
To set a state value:
see also: Updating variables to D8 State system