$sum = valueSum('SK01');
put('SK02_01', $sum);
Below you will find another example that makes the result of a random draw available for the analysis by using ''put()''.
==== Use with Urns ====
The use of urns can be read in detail in the chapter [[:en:create:random_urns]].
Ballots with several values can be stored in an urn -- in some circumstances, this means multiple values have to be stored in multiple internal variables. The function ''[[:en:create:functions:urndraw]]'' expects the ID of the question accordingly. In turn, this question has to contain enough variables in order to be able to store the combination drawn.
The following PHP code draws a ballot out of the urn "urn" and stores the combination in the internal variable in question "IV01".
urnDraw('urn', 'IV01');
In the simplest and most common scenario in that there are only ballots with one each number each in the urn, the question only requires one internal variable. Nevertheless, the function ''urnDraw()'' requires the question ID, not the variable ID.
==== Use with JavaScript ====
If a question type "internal variables" is dragged into the questionnaire, it will not be represented as a question and so nothing can be seen. However, there are two effects in doing so:
- Hidden (''%%type="hidden"%%'') form fields with the IDs of the internal variables will be created on the questionnaire page.
- When the page is submitted with the "Next" button, the values from the hidden form fields will be read and stored in the data record.
The hidden form fields can be accessed by using JavaScript. This means responses or measured values gathered with JavaScript can be conveniently stored in the data record.
The following example assumes that a question type "internal variables" with ID "PC01" was created with two variables in it, which have IDs "PC01_01" and "PC01_02". The hidden form element for variable "PC01_01" can be accessed in JavaScript by using ''%%document.getElementById("PC01_01")%%''. The width and height of the screen should be generated (in [[:de:glossary#pixel|pixels]]) via JavaScript, and stored in the data record.
In addition, question "PC01" is dragged onto the questionnaire page, and the following JavaScript code is placed underneath it as a text element.
===== PHP Variables =====
In PHP, variables start with a dollar sign, e.g. ''$version''. A value is assigned to the variable by using a single equals sign (''=''). This value is compared with another value by using a double equals sign (''=='').
**Note:** A PHP variable is only valid within a //PHP code// element. It is transient, and therefore will not be stored permanently. If you want to use the variable later on in other PHP code elements, the variable therefore has to be made available by using ''[[:en:create:functions:registervariable|registerVariable()]]''. If the value of the variable is needed for the analysis, it has to be stored in an internal variable by using ''[[:en:create:functions:put|put()]]''.
In the following example, the function ''[[:en:create:functions:random|random()]]'' rolls number 1 or 2. The result of this random draw is stored in the variable ''$version''. Afterwards, an IF construction ([[:en:create:filters]]) is used to compare if it is value 1 (then the text element "stimulus1" will be displayed) or not (then "stimulus2" will be displayed).
$version = random(1, 2);
if ($version == 1) {
text('stimulus1');
} else{
text('stimulus2');
}
==== Save in the Data Record ====
In order to save a variable's content beyond the questionnaire, use an internal variable (see above), combined with the function ''[[:en:create:functions:put|put()]]''.
In the example above, the stimulus that was displayed __cannot__ be seen with the data record. Create an internal variable (e.g. "IV01_01") and use ''put()'' in order to save the result of the random draw in the data record:
$version = random(1, 2);
put('IV01_01', $version);
if ($version == 1) {
text('stimulus1');
} else{
text('stimulus2');
}
==== Working with Variables ====
Variables allow you to calculate superbly. In the following example, the participant is asked question "AB01" on page 1 -- a text input for a number. The response is read on page 2 using ''value()'' and stored in the variable ''$number''. This variable is multiplied by 2 (''*'') and the result is stored in the variable ''$result''.
Subsequently, the variable ''$result'' is treated as text (string) and linked with two other string (''.''). The result of this link is transferred to the function ''html()'' to display in the questionnaire.
// Calculate on page 2 with the given number
$number = value('AB01'); // assign given number to the variable $number
$number = (int)$number; // make sure that it is a number
$result = 2 * $number; // multiple number by 2
// Display as text in the questionnaire
html('You have specified half of '.$result.'
');
==== Use Variables Continuously ====
Sometimes it is necessary to use one PHP variable in multiple PHP code elements. In the following example, 10 television stations will be queried in a random order. In question AB01 the participant will be asked how often they watch the stations -- question AB02 will enquire as to how appealing they find the program schedule.
It makes sense that the stations are queried in the same order in both questions. In order to do so, first of a list ([[:en:create:array|Array]]) of the item IDs in a random order is created using ''[[:en:create:functions:random_items|random_items()]]''. This list is stored in the variable ''$items''.
The variable ''$items'' is not only needed on the current page, but on the following page for question "AB02" as well. This is therefore made accessible for future PHP code elements using ''[[:en:create:functions:registervariable|registerVariable()]]''. Subsequently, question "AB01" will be displayed in the shuffled order.
$items = random_items('AB01'); // randomized order of items in question AB01
registerVariable($items);
question('AB01', $items); // ask question with randomized items
''$items'' can then simply be used on page 2.
question('AB02', $items); // ask question with randomized items.