Conditional Macros¶
Built-in functions for testing conditions and making logical comparisons.
Comparison Operators¶
Equality¶
if ($a == $b) { // Loose equality (type coercion)
// Both values are equal
}
if ($a === $b) { // Strict equality (exact type and value)
// Both values and types are identical
}
Inequality¶
if ($a != $b) { // Not equal
// Values are different
}
if ($a !== $b) { // Not strictly equal
// Values or types are different
}
Numeric Comparisons¶
if ($a < $b) { // Less than
if ($a > $b) { // Greater than
if ($a <= $b) { // Less than or equal
if ($a >= $b) { // Greater than or equal
Logical Operators¶
&& (AND)¶
Both conditions must be true.
if ($age >= 18 && $registered == true) {
feedback = "You are eligible.";
}
|| (OR)¶
At least one condition must be true.
if ($month == "December" || $month == "January") {
feedback = "It's winter (in Northern Hemisphere).";
}
! (NOT)¶
Negate a condition.
if (!$isComplete) {
feedback = "Your assignment is not complete.";
}
if ($answer != 42) {
feedback = "That's not the answer.";
}
If/Else Statements¶
Simple If¶
if ($score >= 90) {
feedback = "Excellent work!";
}
If/Else¶
if ($answer == $correct) {
feedback = "Correct!";
} else {
feedback = "Try again.";
}
If/Elseif/Else¶
if ($score >= 90) {
feedback = "A: Excellent";
} elseif ($score >= 80) {
feedback = "B: Good";
} elseif ($score >= 70) {
feedback = "C: Satisfactory";
} else {
feedback = "Below 70: Needs improvement";
}
String Comparisons¶
Case-Sensitive¶
if ($answer == "Paris") {
feedback = "Correct capital!";
}
Case-Insensitive¶
if (strtolower($answer) == strtolower("paris")) {
feedback = "Correct (case-insensitive)!";
}
Substring Search¶
if (strpos($answer, "Darwin") !== false) {
feedback = "You mentioned Darwin correctly.";
}
Array Operations with Conditionals¶
Check if Value in Array¶
$correct_answers = ["Paris", "London", "Berlin"];
if (in_array($studentAnswer, $correct_answers)) {
feedback = "That's a correct European capital!";
}
Count Array Elements¶
$responses = ["A", "C"];
if (count($responses) == 2) {
feedback = "You selected 2 options.";
}
Ternary Operator¶
Shorthand for if/else:
$message = ($age >= 18) ? "Adult" : "Minor";
$result = ($correct) ? "Excellent!" : "Try again";
Examples¶
Feedback Based on Magnitude of Error¶
$correctAnswer = 25;
if ($studentAnswer == $correctAnswer) {
feedback = "Perfect!";
} elseif (abs($studentAnswer - $correctAnswer) <= 2) {
feedback = "Very close! Check your rounding.";
} elseif (abs($studentAnswer - $correctAnswer) <= 5) {
feedback = "In the right ballpark. Review your calculation.";
} else {
feedback = "Not quite. Start over and check each step.";
}
Conditional Hints¶
$attempts = $attemptNumber;
if ($attempts == 0) {
hint = "Try using the distance formula.";
} elseif ($attempts == 1) {
hint = "Distance = √((x₂-x₁)² + (y₂-y₁)²)";
} elseif ($attempts >= 2) {
hint = "The answer is " . $answer;
}
Validating Student Response¶
if (strlen($answer) < 5) {
feedback = "Your answer is too short.";
} elseif (strlen($answer) > 100) {
feedback = "Your answer is too long.";
} elseif (strpos(strtolower($answer), "yes") !== false) {
feedback = "Good, you acknowledged the question.";
}
Conditional Question Display¶
@if ($studentLevel == "beginner")
Solve: x + 5 = 12
@elseif ($studentLevel == "intermediate")
Solve: 2x - 3 = 15
@else
Solve: 3x² - 11x + 6 = 0
@endif
Checking Multiple Conditions¶
if ($age >= 18 && $score >= 80 && !$alreadyPassed) {
feedback = "You qualify for the advanced program!";
} elseif ($age < 18) {
feedback = "You must be 18 or older.";
} elseif ($score < 80) {
feedback = "You need a score of at least 80.";
} elseif ($alreadyPassed) {
feedback = "You've already completed this level.";
}
Operator Precedence¶
Order of evaluation (highest to lowest):
1. ! (NOT)
2. *, /, % (Multiplication, division, modulo)
3. +, - (Addition, subtraction)
4. <, >, <=, >= (Comparisons)
5. ==, !=, ===, !== (Equality)
6. && (AND)
7. || (OR)
Use parentheses to override:
if (($a || $b) && $c) { // Parentheses matter
Common Mistakes¶
- Using
=instead of==:if ($a = 5)assigns; useif ($a == 5)for comparison - Forgetting else cases: Always consider what happens if the condition is false
- String comparison mistakes:
"10" > "2"is false (alphabetical comparison) - Loose vs strict equality:
0 == falseis true;0 === falseis false
See Also¶
- Feedback Macros — Using conditionals in feedback
- Control Syntax — Loops and advanced logic
- Macros Overview — All macro categories