Skip to content
Classroom

String

Answer type: string

The student types short text, graded by comparison rather than by evaluation.

The minimum

Common Control:

$answer = "Paris"

No [string] section

Earlier versions of this page showed a bracketed section — and an earlier commit "corrected" it from [essay] to [string], which replaced one invention with another. There are no bracketed sections; the type is chosen in the editor.

Options

Variable Effect
$answer the accepted text — required
$strflags how strictly it is compared; see below
$ansprompt text before the input box
$answerboxsize width of the input box
$scoremethod takeanything (full credit for any non-blank answer) or takeanythingorblank (full credit even if blank)
$answerformat list (comma-separated answers, any order, partial credit), or logic / setexp (logic or set expressions — both need $variables, e.g. $variables = "p,q")
$displayformat the input control: select or typeahead (both need a $questions array), usepreview, alignright

$strflags

A comma-separated list. Each entry is a bare flag name, which turns it on, or name=value.

Flag Effect
ignore_case case-insensitive comparison — on by default; ignore_case=0 turns it off
trim_whitespace ignore leading and trailing spaces
compress_whitespace treat runs of spaces as one, and trim — on by default; compress_whitespace=0 turns it off
remove_whitespace ignore spaces entirely
ignore_commas ignore commas
ignore_order ignore the order of the characters, not of words: listen accepts silent. Under the default compress_whitespace, an answer containing a space then never matches, not even itself; add remove_whitespace, and cat dog accepts dog cat (and also tac god)
in_answer accept if the answer appears anywhere inside the student's text (photosynthesis accepts it is photosynthesis); any value, even in_answer=0, turns it on
special_or without this flag, or in $answer already separates alternatives (cat or dog accepts dog); special_or switches the separator to *or* with a space on each side (cat *or* dog), so an answer can contain the word "or"
partial_credit award partial credit for a partial match
allow_diff=N tolerate up to N character differences — for spelling
ignore_symbol=X replace X with a space in both texts (well-known then accepts well known but not wellknown; add remove_whitespace to drop it entirely)
regex=1 treat $answer as a regular expression (unanchored, so use ^…$; case-insensitive unless ignore_case=0); the flag's own value is ignored

ignore_symbol, allow_diff and regex require a value; giving them bare prints Missing = in strflag.

$answer = "New York"
$strflags = "ignore_case, compress_whitespace, allow_diff=1"

ignore_case and compress_whitespace are on by default

With no $strflags at all, paris is accepted for Paris and runs of spaces count as one. Turn a default off with =0: $strflags = "ignore_case=0" makes CO wrong for Co. Decide deliberately which differences are part of what you are assessing — spelling usually is not, unless the question is about spelling.

Example

Common Control — setup:

$capitals = array("France" => "Paris", "Japan" => "Tokyo", "Peru" => "Lima")
$country = randfrom(array("France", "Japan", "Peru"))
$city = $capitals[$country]

Question Text:

<p>What is the capital of $country?</p>

Common Control, continued — answer and options:

$answer = $city
$strflags = "ignore_case, trim_whitespace, allow_diff=1"
$ansprompt = "Capital: "
$answerboxsize = 20

See Also

  • Essay — long-form writing, marked by hand
  • Multiple Choice — when a fixed option list is fairer than free text
  • String Macros — stringclean, stringpos for conditional grading