Differences

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

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Last revision Both sides next revision
en:create:php [26.01.2017 07:55]
cofrin [Variables]
en:create:php [06.06.2017 13:26]
admin [Variables]
Line 24: Line 24:
 ===== Functions ===== ===== Functions =====
  
-comupter is a machine and will always do what you tell it to. So you have to tell it pretty exactly what you want. This is called a command. Senseful strings of commands are programs.+computer is a machine and will always do what you tell it to. So you pretty much have to tell it exactly what you want. This is called a command. Senseful strings of commands are programs.
  
 Commands in PHP are called functions. A function looks like this: Commands in PHP are called functions. A function looks like this:
Line 48: Line 48:
 ===== 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