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 |
** or ^ |
Exponentiation | $a ** 2 |
% |
Modulo | $a % 3 |
Built-in Math Functions¶
abs($x) // absolute value
sqrt($x) // square root
pow($a, $b) // $a raised to $b
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) // natural log
log10($x) // base-10 log
exp($x) // e^x
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 and while for iteration:
// Build a sum
$total = 0;
for ($i = 1; $i <= $n; $i++) {
$total = $total + $i;
}
// Build an array
$arr = array();
while (count($arr) < 5) {
$arr[] = randint(1, 10);
}
Arrays¶
Create and work with arrays:
$arr = array(2, 4, 6, 8);
$first = $arr[0]; // 0-indexed
$len = count($arr); // length
$arr[] = 10; // append
sort($arr); // sort ascending
String Operations¶
$s = "hello";
$upper = strtoupper($s); // "HELLO"
$len = strlen($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. Variables do not persist between attempts — setup code re-runs on each new attempt, producing new random values.