Sign in Registration
ArticlesDownloadServicesAudioContacts
ruen

Drupal persistent variables, how the variable set function works

It is customary to use databases or files to store data. But sometimes there is no desire to create a separate table in the database, or it is impractical, and it is not convenient to store data in a file. Then the developer thinks about other ways of storing project information.

drupal-variable-table

Drupal can be used to store persistent variables . It is a fast solution for saving almost any data. First you need to prepare the data, then just call the variable_set function. This function takes two parameters, the first is the name of the variable, the second is its value. The variable name should be in capital letters in English, the value can be any variable, for example, a serialized array.

  variable_set ('variable_name', 'variable_value');  

When the variable is saved, it can be retrieved later using the variable_get function. It is enough to specify the name of the resulting variable and the default value in the parameters - it will be used if such a variable has not been set.

  variable_get ('variable_name', 'default_value');  

To remove a previously set variable, you can use a special function - variable_del . It is enough to specify the name of the variable in the parameter.

variable_del('variable_name'); 

How does the variable_set function work? It simply stores the value in the Drupal database, in the variable table. This table has two columns - the name of the variable and its data in the form of blob . Before saving, the data is serialized if necessary. It is important to understand that variable_set can interfere with the implementation of multilingualism on the site, since you need to take care of translating data from this table yourself.

What other disadvantages of variable_set are there? You should use this function only for storing small data, and not for any large amounts of information, although this function allows you to do this. It is not recommended to do this, since every time the page is requested, all variables are loaded from the database, even if you did not call variable_get . Imagine how this can slow down the speed of a site if huge values ​​were stored and there are many such variables. The variable_get function just gets ready-made information from the data already loaded earlier.

How to get a list of all variables set by variable_get ? To do this, again, you can refer to the Drupal table - variable . See what values ​​are in your project and what values ​​are there after a fresh installation of Drupal. You can quickly do a fresh install of Drupal or view this table on the Drupal table description site. This will allow you to see the differences and be able to list all the custom variables you have set. Then you can, for example, delete all of them using variable_del .

Thus, we have examined the structure of the variable_set function . It can be useful for storing small data, for storing large volumes it is better not to use it, but to create a separate table in the database.

Comments (0)
For commenting sign in or register.

Latest articles

Popular sections

Communication

Login to the web version
Android app:
Available on Google Play

Share this

Subscribe to

YouTube