Challenge 3 — Solution
Task: Write code comparing the integer 1 and the string "1" using both
== and ===, echoing a clear message explaining what each comparison
returned and why they differ.
";
} else {
echo "== says they are NOT equal.
";
}
if ($intOne === $strOne) {
echo "=== says they are equal.
";
} else {
echo "=== says they are NOT equal (strict comparison also checks the type, and int != string).
";
}
?>
Output:
== says they are equal (loose comparison juggles the string to an int first).
=== says they are NOT equal (strict comparison also checks the type, and int != string).
Notes:
- == juggles "1" into the integer 1 before comparing, so the values
match and the comparison is true.
- === refuses to juggle anything - it requires both the value and the
type to already match. Since one operand is an int and the other is
a string, they can never be === equal no matter what the values are.
- This is exactly the distinction the chapter's own warn-box flags as
the single most important type-juggling habit to build early.