Module variable get, set, delete

Get, set, and delete variables using Module variable. Create an object from class module_variable as described in Module variable start. For this example we have an imaginary module named crack_password and we create object $mv.
$mv = new module_variable('crack_password');

Set and get

The following code sets a variable named beta to the value 42 then gets the value from the variable and displays the value.
$mv->set('beta', 42);
print $mv->get('beta');
The result is 42, the value we stored.
42

Get expanded

The following code looks for a variable named alphathen displays the result including the data type.
$alpha = $mv->get('alpha');
print serialize($alpha);
Here is the result serialised to show the exact result. The result is null because the variable does not exist and we did not supply a default.
i:0;
The following code looks for a variable named alpha and supplies a default of zero, 0.
$alpha = $mv->get('alpha', 0);
print serialize($alpha);
The result is zero because the variable does not exist and we supplied the default of zero.
i:0;

Set expanded

The following code sets a variable named beta to the value 42 then displays the value of a variable.
$mv->set('beta', 42);
print serialize($mv->get('beta'));
The result is 42, the value we stored.
i:42;

The semantic Web

The following code displays the value of a variable Beta instead of beta, a simple mistake.
 print serialize($mv->get('Beta')); 
The result is null because the variables are stored in an array and array entry identification is not semantic, it treats the identifier as a string instead of a name. A capitalised letter is stored as a different character to a normal letter.
N;
The Module variable code could be expanded to translate all variable names from upper case to normal case, something I do in some implementations. The result would be one step further away from primitive Unix style processing and one step closer to the Semantic Web.

Delete

The following code sets a variable named beta to the value 85 then deletes the variable then tries to display the variable.
$mv->set('beta', 85);
$mv->delete('beta');
print serialize($mv->get('beta'));
The following result shows the variable is missing and the get function returns null.
N;

Changes are saved

Your changes are saved automatically. They are saved at the point where your module requests a change.