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 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.
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.
- 09.11.23IT / Database Errors when migrating from MySQL 5.6 to 5.7 and how to fix them - database dump import failed with an error or INSERT does not work. Disabling STRICT_TRANS_TABLES strict mode or using IGNORE
- 09.07.22IT / Misc Convert office files DOC, DOCX, DOCM, RTF to DOCX, DOCM, DOC, RTF, PDF, HTML, XML, TXT formats without loss and markup changes
- 07.07.22IT / Safety How to protect PHP, JS, HTML, CSS source code - obfuscation, minification, compression and encryption
- 06.07.22IT / Safety Connection not secure, problem with Lets Encrypt - how to fix expired 09/30/2021 DST Root CA X3, remove it manually and install ISRG Root X1. Example on MS Windows 7
- 08.07.21IT / Misc How to make a free translation for a website without an API, translate documents in Google Translate