if (value('TF03') == 1) { // analog or ISDN?
question('IN15'); // question about tariff
} else {
// in the 'else' part there is a new filter (nested)
if (value('TF03') == 2) { // DSL connection?
question('IN16'); // question on DSL connection
} else {
// in the 'else' part there is a new filter (nested)
if (value('TF03') == 3) { // other connection?
question('IN17'); // question on cabel
} else {
html('You have not given an answer!
');
}
}
}
These nested constructions can be useful -- but in this example it also works more concisely.
===== Continue IF and ELSE =====
The following construction returns the exact same result as in the previous example. It can also be extended indefinitely.
// example for else if - not nested
if (value('TF03') == 1) { // analog oder ISDN?
question('IN15'); // question about tariff
} else if (value('TF03') == 2) { // DSL connection?
question('IN16'); // question on DSL connection
} else if (value('TF03') == 3) { // other connection?
question('IN17'); // question on cable
} else {
html('You have not given an answer!
');
}
===== ELSE + IF = ELSEIF =====
The ''elseif'' command in PHP can be used to simplify this. The statement after this command is then executed if the condition after "elseif" is true, but none of the conditions preceding it are. In practice, ''elseif'' is no different to the ''else if'' in the example above.
// PHP code – example for elseif
if (value('TF03') == 1) { // analog or ISDN?
question('IN15'); // question about tariff
} elseif (value('TF03') == 2) { // DSL connection?
question('IN16'); // question on DSL connection
} else { // none of the previous?
// if a question response was required for the question
// this saves checking the last condition
question('IN17'); // question on cable
}
The value from TF03 can also be saved in advance in a variable ([[variables#php-variablen|PHP Variables]]). This makes it easier again:
// save value in variable
$cn = value('TF03');
if ($cn == 1) { // analog or ISDN?
question('IN15'); // question about tariff
} elseif ($cn == 2) { // DSL connection?
question('IN16'); // question on DSL connection
} else { // none of the previous?
question('IN17'); // question on cable
}
===== SWITCH =====
If, as in the example, only different values of a variable should be called up, it is obviously somewhat long-winded to write ''value('TF03')'' each time. Instead of doing this, the ''switch'' construction in PHP can be used.
To do this, the value to be checked is stated in brackets behind the ''switch''. The different options are written after each ''case'', followed by a colon.
**Note:** There must always be a ''break'' after the command (in contrast to the ''elseif'' construction) -- otherwise __everything from__ ''case'' __onwards__ will be executed.
switch (value('TF03')) {
case 1:
question('IN15'); // question about tariff
break;
case 2:
question('IN16'); // question on DSL connection
break;
case 3:
question('IN17'); // question on cable
break;
default: // default used, if no case applies
html('Invalid Answer
');
break; // the last break is not necessary
}
// other questions which all respondents are asked
question('BB01');
Of course, this can be written in such a way to take up less space:
switch (value('TF03')) {
case 1: question('IN15'); break; // question about tariff
case 2: question('IN16'); break; // question on DSL connection
case 3: question('IN17'); break; // question on cable
default: html('Invalid Answer
');
}
// other questions which all respondents are asked
question('BB01');