Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:create:php [22.02.2017 10:26] – [Functions] aitorme.uni-saarlanden:create:php [15.04.2020 20:28] (current) admin
Line 4: Line 4:
  
 This chapter tries to give a short introduction to using PHP. Not so much a broad 101 to programming PHP, but more the basics you need for advanced questionnaires and filters. This chapter tries to give a short introduction to using PHP. Not so much a broad 101 to programming PHP, but more the basics you need for advanced questionnaires and filters.
 +
 +**Tip:** [[https://www.soscisurvey.de/homepage/index.php?id=screencast#extra|Video Tutorial on PHP Code]]
  
 ===== Using PHP code in the questionnaire ===== ===== Using PHP code in the questionnaire =====
  
 To use PHP in the questionnaire, you need to **compose a questionnaire** first. If one is open, ready for editing, you'll see the pages at the top, the current questionnaire page on the left, and the building blocks for the questionnaire on the right: questions, text blocks, etc. To use PHP in the questionnaire, you need to **compose a questionnaire** first. If one is open, ready for editing, you'll see the pages at the top, the current questionnaire page on the left, and the building blocks for the questionnaire on the right: questions, text blocks, etc.
 +
 One of those building blocks is the block "PHP-Code". You can drag-and-drop it into the questionnaire page, just like the questions and text blocks. One of those building blocks is the block "PHP-Code". You can drag-and-drop it into the questionnaire page, just like the questions and text blocks.
  
Line 13: Line 16:
  
 After inserting the //PHP-Code//-element into the page, you will see a text entry field that will accept PHP-code. After inserting the //PHP-Code//-element into the page, you will see a text entry field that will accept PHP-code.
 +
 +**Note:** The PHP code will be executed as soon as the page is reached during filling out the questionnaire, or if the page is run in the preview.
 +
  
 ===== Comfortable programming (questions and texts) ===== ===== Comfortable programming (questions and texts) =====
Line 48: Line 54:
 ===== Variables ===== ===== Variables =====
  
-The word //function// derives from mathematics. Although the command ''question()'' has not much in common with functions. But other functions can actually compute things. For example, the command ''[[:en:create:functions:random|random()]]'' computes a random number.  +The word //function// derives from mathematics. Although the command ''question()'' has not much in common with functions. Yet, other functions can actually compute or determine things. For example, the command ''[[:en:create:functions:casenumber|caseNumber()]]'' determines the number of the current interview case
- +
-The function ''random()'' expects two parameters: the smallest and the largest number that can be drawn. ''random(1,6)'' will draw a number between 1 and 6. We can use this number, e.g. to choose a stimulus randomly.+
  
 <code php> <code php>
-$number = random(1,6);  // Draws a number between 1 and 6+$number = caseNumber();
 </code> </code>
  
-To use the output of this function (in this case the random number), we need to file it somewhere. To do so, most programming languages use variables. You can write anything into a variable. But at the beginning, no one will see this.+To use the output of this function (in this case the interview number), we need to file it somewhere. To do so, most programming languages use variables. You can write anything into a variable. But at the beginning, no one will see this, and it will not be stored in the data set.
  
-In PHP, variables start wit a dollar-sign ($). The example above uses the variable ''$number''. You can name your variables whatever you like -- except for the fact, that you must not use most special signs (!"§$%&/()=...), apart from the underscore (_). You could name the variable ''$random_number_variable'', too.+In PHP, variables start wit a dollar-sign (''$''). The example above uses the variable ''$number''. You can name your variables whatever you like -- except for the fact, that you must not use most special signs (!"§$%&/()=...), apart from the underscore (''_''). You could name the variable ''$interview_number_variable'', too.
  
-The equal sign (%%=%%) is an operator. In this case it makes sure, the output of the function  ''random()'' is saved in the variable ''$number'' on the left. So the variable ''$number'' now contains  a random number.+The equal sign (%%=%%) is an operator. In this case it makes sure, the output of the function  ''caseNumber()'' is saved in the variable ''$number'' on the left of the operator. So the variable ''$number'' now contains the interview number.
  
-If we want to print the random number onto the screen, we need the command ''[[:en:create:functions:html|html()]]''. This function does nothing else than to print HTML-code (which is, basically, text) into the questionnaire.+If we want to print the interview number onto the screen, we need the command ''[[:en:create:functions:html]]''. This function does nothing else than to print HTML-code (which is, basically, text) into the questionnaire.
  
 <code php> <code php>
-$number = random(1,6);  // Draws a number between 1 and 6 and saves it into $number+$number = caseNumber();
 $text = (string)$number; // Converts the number into text and saves it into $text $text = (string)$number; // Converts the number into text and saves it into $text
-html($text);  // Prints the text $text, which contains the random number+html($text);  // Prints the text stored in $text
 </code> </code>
  
-**Advice:** Why we need to convert the number into text: You could also write ''html($number)'' -- which will work. But as SoSciSurvey expects to output //text// with ''html()'', it will be irritated, why you want to output a //number// (which is usually used for computing, not output). It will simply print an error message to advise you about the (alleged) mistake.+**Advice:** Why we need to convert the number into text: You could also write ''html($number)'' -- which will work. But as SoSci Survey expects to output //text// with ''html()'', it will be irritated, why you want to output a //number// (which is usually used for computing, not output). It will simply print an error message to advise you about the (alleged) mistake.
  
-If copying the above code into a PHP-code-field on a questionnaire page, you will see an unspectacular outcome: A random number between 1 and 6. When reloading the pagea different number will be shown.+If copying the above code into a PHP code-field on a questionnaire page, you will see an unspectacular outcome: A number, increasing with every new interview that is started.
  
-{{:en:create:scr.php.random.png?nolink|Display of a random number in the questionnaire}}+{{:en:create:scr.php.random.png?nolink|Display of a number in the questionnaire}}
  
-How random numbers are of use to you, is described in the chapter [[randomization]]. For the moment, we will concentrate on variables: You can save values into them and call those values later on.+You will probably not need the function ''caseNumber()'' very often. For the moment, we will concentrate on variables: You can save values into them and call those values later on.
  
 ===== Concatenating strings/texts ===== ===== Concatenating strings/texts =====
en/create/php.txt · Last modified: 15.04.2020 20:28 by admin
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International
Driven by DokuWiki