Control Syntax¶
Question code uses a PHP-like scripting language to compute values, randomize content, and build conditional question text.
Variable Assignment¶
Variables are prefixed with $. Assign values with =:
$x = 5;
$name = "linear";
$values = array(1, 2, 3);
Arithmetic¶
Standard math operators work as expected:
| Operator | Meaning | Example |
|---|---|---|
+ |
Addition | $a + $b |
- |
Subtraction | $a - $b |
* |
Multiplication | $a * $b |
/ |
Division | $a / $b |
^ |
Exponentiation (rewritten to safepow; PHP's ** also parses but gives NAN for a negative base with a fractional exponent) |
$a ^ 2 |
% |
Modulo | $a % 3 |
Built-in Math Functions¶
abs($x) // absolute value
sqrt($x) // square root
$a^$b // $a raised to $b (rewritten to safepow)
floor($x) // round down
ceil($x) // round up
round($x, $n) // round to $n decimal places
sin($x) // sine (radians)
cos($x) // cosine
tan($x) // tangent
log($x) // base-10 log (the interpreter rewrites log to log10)
ln($x) // natural log
nthlog($b, $x) // log base $b
exp($x) // e^x
pi // π (a constant: write pi, not pi())
Conditional Statements¶
Use if, elseif, and else to branch logic:
if ($a > 0) {
$sign = "positive";
} elseif ($a < 0) {
$sign = "negative";
} else {
$sign = "zero";
}
Loops¶
Use for ($i = a..b) { ... } to loop over whole numbers from a to b inclusive (at most 1000 iterations), and foreach ($arr as $k=>$v) { ... } to loop over an array. C-style for (...; ...; $i++) loops and while are not supported: either one turns the whole setup block into an error. To repeat a random draw until a condition holds, use where.
// Build a sum
$total = 0;
for ($i = 1..$n) {
$total = $total + $i;
}
// Build an array
$arr = array();
for ($i = 1..5) {
$arr[] = rand(1, 10);
}
// Repeat a draw until a condition holds
$b = rand(-5, 5) where ($b != 0)
Arrays¶
Create and work with arrays:
$arr = array(2, 4, 6, 8);
$first = $arr[0]; // 0-indexed
$len = count($arr); // length
$arr[] = 10; // append
$arr = sortarray($arr); // sort ascending (returns the sorted array)
String Operations¶
$s = "hello";
$upper = strtoupper($s); // "HELLO"
$len = stringlen($s) // 5
$combined = $s . " world"; // concatenation
$sub = substr($s, 1, 3); // "ell"
Displaying Variables in Question Text¶
Use $varname directly in the question text HTML. For math display, wrap in backticks:
<p>If `a = $a` and `b = $b`, find `a + b`.</p>
For computed expressions, you can compute them in setup and display the result:
$result = $a + $b;
<p>The answer is `$result`.</p>
Scope¶
All variables defined in setup code are available in question text, answer evaluation, hints, and solution text. Setup code re-runs every time the question is displayed or scored, using the question version's stored seed, so the same version always produces the same values, including on every retry of that version. New random values appear only when a new version of the question is generated (or a new assessment attempt starts), and assessment settings can change that. All items same random seed gives every question in an attempt the same seed, but a new attempt still draws a new one unless All students same version of questions is also ticked. A question whose Restricted question seed list has only one seed shows the same values every attempt, unless All items same random seed is ticked, which ignores the list.