Which question is not worth answering

Filter questions (PHP filter)

If the participant's answer to a question should have an impact on the further course of the interview, it is a filter question. You can either define filters directly in the question (Question filter, simple variant) or under Compile a questionnaire (PHP filter) apply.

PHP filters allow much more sophisticated filters than question filters, but require a little PHP program code to be used. Most SoSci Survey users have never worked with a programming language before. To create filter questions, you will learn some basic knowledge of programming with PHP in this chapter. No fear: It's hard to get started, but programming filters is perfectly feasible and the reward is a steep learning curve. A happy aha experience usually compensates for any frustrations.

Important: To understand this chapter, the chapter Introduction to PHP Code is strongly recommended.

Important: If a filter does not work as intended, see Troubleshooting filter questions

Tip: If the participants are divided into control and experimental groups (random generator, randomization) and see different questionnaire pages, it usually makes sense to work with instead of: The earlier SoSci Survey knows in the course of the questionnaire that pages are being skipped, the more accurately it can the progress bar can be adjusted.

Practical example: yes-no filter

Before filters and their function are explained in detail, here is the solution for those who have little time. A yes-no filter is probably the most common use for filters.

Example 1: If a participant answers "yes", an additional question should be displayed.

Example 2: If a participant answers "no", several pages are skipped.


You will need the following information to install the filter:

  • The identifier of the variable in which the yes-no answer is stored,
  • the answer codes of the question and
  • the identifier of the question which should be displayed in the event of a "yes" (example 1)
  • the side where it continues with a "no" (example 2)

The identifier of the variable can be found in the Variables overview. It says something like this:

[PT01] Dropdown selection TV ownership PT01 TV ownership 1 = yes 2 = no -9 = no answer

The variable is called PT01, the answer code for "yes" is 1 and the code for "no" is 2. Ideally, you have specified for the filter question that the participant has to answer it (tab Examination of the answer). Then the code -9 can no longer occur.

Note: If there are several variables there, e.g. and, check the type of question. It is important that you use the "Choice" or "Dropdown Choice" question type for easy selection - Not but "multiple selection" if you do not want to allow any.

Insert PHP code

The filter is now saved as a PHP code at the Compile a questionnaire entered. Namely at the point where the filter should be active (i.e. where the second question is displayed or where the jump is made). To do this, simply pull in with the mouse PHP code from the right into the questionnaire page (introduction to PHP code).

Important: A PHP filter can No way are on the same page as the filter question is on (i.e. the question whose answer you are referring to, in the example). SoSci Survey always processes the questionnaire page by page in order to display a complete questionnaire page to the participants. At the moment when the page with the filter question is processed, the participant cannot yet have answered this question - and accordingly a filter cannot yet react to the answer ().

Common use cases

If you can use the PHP function, you can use it to implement a number of filters.

Display a question or not

The following PHP code checks whether the response code has been saved for the variable. If so, the question is displayed using.

Note the double equal sign () after the command.

if (value ('PT01') == 1) {question ('AB01');}

Tip: This filter can also be implemented as a question filter without any PHP code.

Tip: If you want to save yourself a little typing, then simply drag the question you want to display into the PHP code from the right. You can then adjust the display options for the question. After confirming with the green check mark, the PHP code for the question will be displayed in the below PHP code Element attached. Then all you have to do is move the code to the right place (between the curly brackets).

Tip: You can also write several commands between the curly braces and.

Show different questions

Depending on an earlier answer (or a group assignment, e.g. through randomization) should question A or question B be shown?

In the following example, depending on the answer to a multi-choice question (AF01), either question FF01 or FF02 is displayed.

if (value ('AF01') == 2) {question ('FF01');} else {question ('FF02');}

Important: The questions FF01 and FF02 are not allowed to be pulled to the side. Otherwise they will be displayed twice: once by the command and once by being dragged to the page.

If the question FF01 is to be displayed for several answers in AF01, then the logical operator is used.

if ((value ('AF01') == 2) or (value ('AF01') == 4)) {question ('FF01');} else {question ('FF02');}

You can also choose from more than 2 questions.

if (value ('AF01') == 1) {question ('FF01');} elseif ((value ('AF01') == 2) or (value ('AF01') == 4)) {question ( 'FF02');} else {question ('FF03');}

Contents vary

Instead of questions, content, e.g. stimuli, can also be varied. More on this in the chapter on randomization. In the following example - depending on the code in variable AF01 - either the text module with the identifier "stimulus1" or "stimulus2" is displayed.

if ((value ('AF01') == 2) or (value ('AF01') == 4)) {text ('stimulus1');} else {text ('stimulus2');}

Skip pages

At the Compile a questionnaire Now select the side where you want to continue with a "no". For this side you wear one Identifier on, e.g. "keineTV" (assign an identifier for a page).

Then go to the page where you did that PHP code Have placed the element for the filter (see above). Write in the following:

if (value ('PT01') == 2) {goToPage ('keinTV');}

This code uses to check whether the value has been saved in the variable. If so, the processing jumps directly to the page with the identifier "keineTV". Subsequent contents of the current page are ignored.

Tip: Read the rest of the chapter as well. It is worth it ..

The IF keyword

Filters are used in your questionnaire by IF-THEN-ELSE relationships in an element PHP code programmed. In the PHP programming language the corresponding keywords are called if (IF-THEN) and else (OTHERWISE).

Behind if there is a so-called condition in brackets. If the condition is met, the command (which in turn follows the condition) is executed:

In addition, you can use a else also specify what to do if the condition is not met:

To make the whole thing clearer (and to allow multiple commands), the commands are written in curly brackets (and). The indentation makes it easier to read.

if (condition) {command 1} else {command 2}

Show questions in questionnaire or not

A filter always has two things: a cause and an effect on the course of the questionnaire.

  • The cause is usually a response from the respondent.
  • The most common effect is that a question or a whole block of questions are displayed or not depending on the answer.

For the following example we assume that the questionnaire asked on page 2 how high the monthly income is. There were several categories to choose from: 1 = no income, 2 = up to 500 EUR, etc. This question is the filter question in the following.

Now it is important to know that SoSci Survey - while page 2 is being created - has no idea what the respondent will answer. The page is first created in full, then displayed to the respondent. He chooses an option and clicks on "Next". Only when you click on “Next” will the answer to page 2 be transmitted to the survey server and saved. Immediately afterwards, the server creates page 3 and sends it to the participants for completion.

The essential thing is that the server does not know a response from page 2 until page 3. Therefore, the filter for the question must be created on page 3 at the earliest.

But what if the questions on page 3 should only be displayed if the first option has been selected? Then you have to think a little around the corner: To do this, the filter directs all respondents who Not have selected the first option, go directly to page 4.

Most of the filters in SoSci Survey work according to this principle: You can skip individual questions or entire pages. Under certain circumstances, several filters have to be hung one behind the other for this:

For example, if you show a multiple selection with 5 car brands on page 10 and would like to ask 4 questions (over 2 pages) about the known brands, then you need 5 filters. The first filter checks on page 11 whether the first item of the multiple selection has been clicked. If not, he jumps directly to page 13 (pages 11 and 12 contain the questions about the first brand). The next filter is on page 13. If the second mark (i.e. the second item) has not been clicked, you go directly to page 15.

If the third mark has been checked, the filter on page 15 does not react. The questions on the page are displayed as normal and SoSci Survey waits for the respondent to click on "Next". Then page 16 is displayed with the two questions. The next filter is only on page 17: Either it forwards the participant directly to page 19 or it doesn't ...


A condition is always in parentheses. It may or may not be fulfilled.

A condition can take several forms. The most important form is the comparison between two values ​​(e.g. two numbers):

  • (two equals characters) checks whether two values ​​are equal,
  • (Exclamation mark and equal sign) checks whether two values ​​are not equal,
  • (greater than) checks whether the first value is greater than the second,
  • (less than) checks accordingly whether the first value is less than the second.

Attention: To check equality, a double equal sign () must be used! A simple equal sign () would attempt to assign a value to a variable.

Tip: If you want to check whether a variable e.g. has the value 1 or 2, please read Linking Multiple Conditions.

Skip pages

Back to the simple example: On page 1 of the questionnaire, question AB01 is asked (a selection on income). The aim is that questions EK01 and EK02 are only asked if the respondent selects the first option for question AB01.

Conversely, this means: If the respondent chooses an answer option greater than 2 (income up to EUR 500), then page 3 is skipped.

The following example describes a questionnaire with 3 pages. On page 2 it is checked whether the person has stated an income of more than EUR 500. If not, the rest of the page is skipped and you go straight to page 3.

Tip: The chapter Introduction to PHP Code describes how to add PHP code to the questionnaire. The short version: For filters you need PHP code. For this you draw an element PHP code (above the selection list with questions) into the page.

Tip: Of course you don't have to insert the questions as PHP commands, you can simply drag them from the selection onto the page when compiling the questionnaire (programming is more convenient).

Assign an identifier for a page in the questionnaire

For the third page you have to enter “tvkonsum” as the identifier, this is the only way to jump directly to page 3 with.

You can specify the identifier when compiling the questionnaire when you have clicked on the page: Simply enter it on the top left of the page.

Implementation with as little PHP code as possible

Implementation with as much PHP code as possible

Normally you will only use PHP code in the questionnaire where you need it - as above. Because the contents of the questionnaire page can be presented so nicely and briefly in PHP code, you will often find the following form in the instructions. In terms of content, the function is identical to the above implementation with little PHP code.

page 1

question ('AB01'); // Selection of monthly income

Page 2

if (value ('AB01')> 2) {goToPage ('tvkonsum'); // Jumps directly to page 3} question ('EK01'); // Study funding question ('EK02'); // Additional income

Page 3 with the identifier "tvkonsum"

// Page 3 must have the identifier "tvkonsum" question ('TK01'); // Daily television duration question ('TK02'); // Favorite channels

Important: Questions can be dragged into the questionnaire page or integrated into the PHP code using the command. The simultaneous inclusion as question elements and in the PHP code ensures that the question is displayed twice and you receive corresponding error messages.

Tips for the value () function

  • There are two easy ways to find out what value is stored for an answer. Once the Variables overview and secondly, the debug mode when testing a questionnaire (compose questionnaire).
  • The function returns the answer from a respondent. However, this only works if the respondent has sent the answer by clicking "Next" - that is, only on the next page.
  • All functions that are specifically provided by SoSci Survey are listed under PHP functions and are explained there.

Instruction blocks

In the example, the IF condition is followed by a curly bracket (). Curly brackets are used in PHP to summarize several functions / commands (e.g. several questions). It generally makes sense to put the instruction (s) behind an IF construction in curly brackets - if you don't use brackets and add further commands, you often look for the error for a long time.

Tip: Use curly braces for each statement block and indent the PHP code with spaces as in the example (the input fields unfortunately still have problems with tabs). In this way, you do not lose track of things, even with more complex filters, and avoid errors.

Example for page 2

if (value ('AB01') <= 2) {question ('AB02'); // Study funding question ('AB03'); // Additional income} else {question ('AB04'); // Working hours per week question ( 'AB05'); // Employment relationship question ('AB06'); // Limitation} question ('AB07'); // Everyone sees this question again

Step by step

This section now explains step by step how to program a small filter.

The filter question

Back to the use of if-then conditions as filter questions: in principle, every question can become a filter question. Whenever the answer to the question is used in a condition. So first a normal question needs to be asked.

  1. Put in Questionnaire a new section, Identifier "TF", description "Test for Filters"
  2. In the rubric place a new question at, description "Filter selection", Type "Selection"
  3. Enter the following in question TF01:
    1. Question text "Do you have an Internet connection at home?"
    2. The question must be answered in full "Yes".
    3. Then please open to save click and
    4. in the Quick entry for items enter two items: "Yes" and "No".

Now take a look at the Variables overview. You will find the following entry there

The identifier for the question is TF01. The question is a selection, so the answers are saved directly under the identifier TF01 and not - as is the case with a scale, for example - in the individual items. The variable overview also reveals which values ​​TF01 can assume: for the answer “yes” and for the answer “no”. In addition, it is still possible (the question was not answered). In fact, that shouldn't happen because we have set that the question needs to be answered. Details are described in the section Coding and return values.

Now create under Compile a questionnaire a new questionnaire, Identifier "Filter1", description "My first filter question".

Tip: The option The question must be answered in full activated. This option should generally be used very sparingly - with filters, however, it makes sense to insist on an answer: Because here you have to make a decision based on the answer.

Filters to choose from

The command is required to use question TF01 as a filter question. This asks you which answer was selected in the questionnaire. Specifically, it would return 1 or 2 (it must be answered yes). The prerequisite is that the question has already been asked and the answer by clicking on Further has been sent and saved.

If question TF01 is asked on page 1 of the questionnaire, a PHP code on page 2 would look like this:

if (value ('TF01') == 1) {question ('IN01'); // Type of Internet connection: Analog, ISDN, DSL, etc. question ('IN02'); // Business use of the connection at home? question ('IN03'); // Internet provider / service provider} question ('AB01'); // Employment relationship

The condition checks whether the answer was equal to 1, ie whether "Yes" was selected. If this condition is met, three questions about home internet use are asked on page 2 (IN01 to IN03).

All respondents - regardless of whether they have an Internet connection at home or not - also received question AB01. People without a connection see only this one question on page 2 - people with a connection a total of 4 questions.

Using else

By using, you could also ask why you don't have a connection at home:

if (value ('TF01') == 1) {question ('IN01'); // Type of Internet connection: Analog, ISDN, DSL, etc. question ('IN02'); // Business use of the connection at home? question ('IN03'); // Internet service provider / service provider} else {question ('IN04'); // Why no internet connection at home?} question ('AB01'); // Employment | ===== Skip pages ===== If you don't use an '' else '' part, the respondent may see a blank page. If the respondent answers "no" on page 1, in the following example on page 2 he would not see any questions, just an empty page: if (value ('TF_01') == 1) {question (' IN_01 '); // Type of Internet connection: Analog, ISDN, DSL, etc. question (' IN_02 '); // Business use of the connection at home? question ('IN_03'); // Internet provider / service provider}

In this and many other cases it is better to use the command. This command jumps directly to the specified page in the questionnaire. In the example above, page 2 is only relevant for people with an Internet connection at home. Everyone else should jump straight to page 3.

In order for the command to work, you have to give the jump target (i.e. the page to which you want to jump) an identifier. In the following example, page 3 has the identifier. You can also jump to the next page with the identifier.

The following examples have the same result, because nothing is done on the page after that. The command leaves the old page and does not come back.

Option 1: skip ahead if there is nothing to ask

if (value ('TF_01') == 1) {question ('IN_01'); // Type of Internet connection: Analog, ISDN, DSL, etc. question ('IN_02'); // Business use of the connection at home? question ('IN_03'); // Internet provider / service provider} else {goToPage ('usage'); // Continue to page 3 with the identifier "usage"}

Option 2: skipping the irrelevant questions

if (value ('TF01')! = 1) {// You could also check here: value () == 2 goToPage ('usage'); // Continue to page 3 with the identifier "usage"} question (' IN_01 '); // Type of Internet connection: Analog, ISDN, DSL, etc. question (' IN_02 '); // Business use of the connection at home? question ('IN_03'); // Internet provider / service provider

As described in the second example, several pages can of course also be skipped.

Tip: After asking for some key data, do you see that a participant does not belong to the target group of the survey? Screenout: Filter unsuitable participants

Tip: Since SoSci Survey version 2.2.02, blank pages are skipped by default. You often don't have to worry about the problem of blank pages.

The right identifier

In the example you used a selection. A simple selection only saves a single value (namely the selected option), which is queried using the question ID (e.g. TF01).

If you use scales, text entries or a multiple selection, you have to use the identifier of the corresponding item (e.g. TF02_02) and not the identifier of the question.

This multiple selection with the identifier TF02 has 4 items. The item with the identifier 3 (TF02_03) asks whether the respondent has DSL access. You can always find the correct identifier in the Variables overview.

The following display is obtained in debug mode if you click on page 1 (displayed on question TF02) Further clicks. In the example, the first and third item were ticked.

In a multiple selection, each item can have the value 1 (not selected) or 2 (selected). If you want to filter on page 2 whether the respondent has a DSL connection, the PHP code looks like this:

Filter for a multiple selection

if (value ('TF02_03') == 2) {// Was item 2 checked? question ('IN10'); // speed of the connection question ('IN11'); // use of video conferences} else {question ('IN09'); // another connection at home?}

A major disadvantage of multiple selection is that you cannot check whether the respondent has answered it. After all, no tick can be a correct answer.

The problem can be avoided by using a 2-point scale instead of multiple selections. If you label the minimum with “no” and the maximum with “yes” (in the figure is the Direction of the scale descending that Alignment of the lettering in the middle), you get the same value for each item as in the multiple selection (1 = no, 2 = yes).

In the chapter at least one item, some tricks are explained how to deal with scales in filters.

Screenout: Filter unsuitable participants

If your questionnaire is only aimed at a certain target group, you may want to say goodbye to unsuitable participants after a few screening questions. To do this, ask the screening questions on the first pages of the questionnaire and then use an ordinary filter.

  • With the command you can send a participant directly to the end of the questionnaire. The interview is marked as "completed" and the participant sees the last page saying goodbye.
  • With a combination of the commands and you can display an individual farewell. The interview will be there Not marked as "completed". You save the farewell text as a text module under Text modules and labeling.
// Screenout with individual textif (value ('AB01')> 3) {text ('screenout'); // Show goodbye buttonHide (); // Hide the next button pageStop (); // If there is still content under the filter follow on the page}

Tip: You can check different variables by repeating the filter. Using Boolean operators, you can also check several variables in a filter at the same time (link several conditions).

Tips on filters

Filters are an extremely flexible tool. They can even be nested, if necessary, or check complex conditions. More on this in further chapters (see below).

Unfortunately, filters don't always work the way you want them to, especially at the beginning. If it doesn't work, there are a few simple ways to track down the problem:

  • See you in the Variables overview whether the identifier of the variable is correct.
  • The filter must not (!) Be on the same page as the filter question. Otherwise the filter will be executed before the participant has sent his answer.
  • Start the questionnaire regularly in debug mode. Then SoSci Survey reveals after every click on Furtherwhich value was saved under which identifier.
  • The debug mode is also very helpful when blank pages appear or commands produce seemingly nonsensical results. For each jump with a line “Create page X” appears in the debug information.
  • Just let SoSci Survey tell you what kind of item / question is currently saved.
    • You can display the value returned by value ()
    • as well as answering the question whether a condition applies (true) or not (false):
  • Jumping to pages with is only useful if you leave out questions. If questions are asked alternatively, it is better to include them directly in the IF block.
  • The command jumps immediately to the specified page (not after clicking on Further) and appends the new page to the current page, if questions were already displayed there.
  • If you jumped to another page with and this page was answered, then it goes from there continue as normal.

More complex filters

Most filters can be programmed with the above basics. Not all of them, though. The following chapters present solutions for more complex filters: