Why this matters
Loose comparisons can yield unexpected results due to type juggling (e.g., "0e123" == 0 is true). Using === ensures both type and value match, preventing subtle bugs and security issues.
Use strict comparison operators (=== and !==) instead of loose (== and !=) in most cases. Strict comparisons check both type and value, avoiding PHP’s implicit type coercion pitfalls.
Loose comparisons can yield unexpected results due to type juggling (e.g., "0e123" == 0 is true). Using === ensures both type and value match, preventing subtle bugs and security issues.
Side-by-side examples engineers can pattern-match during review.
<?php
\$hash = md5(\$input);
if (\$hash == "0e123456789abcdef") {
echo "Hash matches";
}
?><?php
\$hash = md5(\$input);
if (\$hash === "0e123456789abcdef") {
echo "Hash matches";
}
?><?php
\$hash = md5(\$input);
if (\$hash == "0e123456789abcdef") {
echo "Hash matches";
}
?><?php
\$hash = md5(\$input);
if (\$hash === "0e123456789abcdef") {
echo "Hash matches";
}
?>From the same buckets as this rule.
Check if loops use equality operators (== or !=) in termination conditions. These can lead to infinite loops if the condition is never met exactly. Instead, use relational operators like < or > for safer loop termination.