10

I am trying to understand variable_get(). I read so far that you can set variables in the settings.php using the $conf array and retrieve them later in code by using variable_get(). I have read that you can also use variable_get() to retrieve values from the database.

How does this work?
Is there a table that is automatically built?
How would you set variables if this feature is possible?

4 Answers 4

25

Maybe you hope to learn that variable_set is the magical way to store all kinds of structured data in your database, and that variable_get is the way to get it out again. Unfortunately, it is not.

The "persistent variables", as they are often called, are meant for relatively simple key-value pairs that should persist across requests, users and sessions, often until a site admin changes the value. A good example is the site name.

So how does it work?

When Drupal is bootstrapped, all variables are read from the cache (if available) or from the variables table. (See variable_initialize for the exact code.) The variables and stored in the global $conf variable, a huge array. Drupal does this because it's more efficient than doing a database query every time a variable is needed. (It's also the reason why variable_set is not suitable for large blobs of data: they would be loaded on every request!)

Whenever you use variable_get('variable_name', 'default_value'), the variable name is looked up in the $conf array. If it exists, the value is returned; else, the default value is returned.

Finally, there is a third member of the family: variable_del. As the name implies, this deletes a variable from the variables table. This function is often used in hook_uninstall implementations, deleting variables when a module is uninstalled.

If you are looking for ways to store more complex custom data structures in the database, check out hook_schema and drupal_write_record, or use the Drupal entity system and Entity API.

1
  • 7
    Another important note is that variable_set() causes the variable cache to be rebuilt, so it is something that is usually reserved for data that is pretty long lasting and consistent like admin settings and the like. Or, put another way, it shouldn't be used instead of or like static variables between functions that change during the page build.
    – Jimajamma
    Commented May 21, 2013 at 21:54
6

If you are using Drush (strongly recommended), you can issue drush vget > variables.txt from the command line to create a text file with a list of all current variables and their values. Similarly, you can use drush vset to change set any variable to a different value.

See GitHub for more details about variable-get, variable-set, and other useful commands.

3

To set variables, you would use variable_set(). Then you can retrieve them using variable_get(). You should become familiar with the Drupal API site.

The variables, if stored, can be found in the variables table.

1
  • It is not clear from above discussion how to set variable in custom module. So How to set variable from custom module?
    – Fawwad
    Commented Sep 11, 2015 at 5:32
2

You can:

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.