====== registerVariable() ======

''void **registerVariable**(string //variableName//)''

In PHP, a variable is normally only valid within __one__ PHP code element. By using ''registerVariable()'', a variable can be made available for all following PHP code elements.  

  * //variableName//\\ A string (in quotation marks) with the name of the variable. This cannot be the variable itself (e.g. ''$item''), a string with the name of the variable (e.g. 'item').

===== Example 1 =====

In the following example, on page 1, three items are taken from the items in question "AB01". Following this, the question is asked with these three items -- and, on a later page, question "ABO2" is asked with these exact same three items.


<code php>
// Take three items from question AB01
$itemlist = random_items('AB01', 3);
// Show the respective question
question('AB01', $itemlist);
// Register variable $itemlist
registerVariable('itemlist');
</code>

As the variable ''$itemlist'' was made available for other pages in the questionnaire, you can just carry on working with ''$itemlist'' in another PHP code element.

<code php>
question('AB02', $itemlist);
</code>


===== Example 2 =====

On page 4 in the questionnaire, a rather complicated filter is used to determine whether the participant falls into group 1 (prospective clients or clients preparing to do business), or group 2 (current clients). Different questions will be shown later on the in the questionnaire resulting from this classification.

<code php>
if (
  (value('BB01') == 1) or
  ((value('BB02') < 3) and (value('BB04') == 2)
) {
  $group = 1;
} else {
  $group = 2;
}
registerVariable('group');
</code>

The variable ''$group'' can also be used later on in the questionnaire.


<code php>
if ($group == 1) {
  question('BX01');
} else {
  question('BX02');
}
</code>

<code php>
if ($group == 1) {
  question('BX04', '1-3,5,6');
} else {
  question('BX04', '1-5,7,9');
}
</code>

**Tip:** As the group will also be important in the analysis, the use of ''[[:en:create:functions:put|put()]]'' and, on later pages, ''[[:en:create:functions:value|value()]]'' would arguably be more effective here. 



===== Example 3 =====

4 questions ("AB01" to "AB04") are displayed with their associated text elements ("text1" to "text4) -- but in random order. Therefore, an array is shuffled on page 1 of the questionnaire and made available for the following pages with ''registerVariable()''. For further details, please see chapter [[:en:create:rotation|Rotation]].

<code php>
$questions = array(
  array('AB01', 'text1'),
  array('AB02', 'text2'),
  array('AB03', 'text3'),
  array('AB04', 'text4')
);
shuffle($questions);
registerVariable('questions');
</code>

On the following four pages, each question and text element, which can be found in the corresponding position in the array, is displayed. 


<code php>
text($questions[0][1]);
question($questions[0][0]);
</code>

<code php>
text($questions[1][1]);
question($questions[1][0]);
</code>

<code php>
text($questions[2][1]);
question($questions[2][0]);
</code>

<code php>
text($questions[3][1]);
question($questions[3][0]);
</code>