Why this matters
Each allocated resource consumes memory or system handles. Not releasing them can cause leaks or exhaustion (e.g., max DB connections). Proper cleanup ensures efficient operation and prevents resource‐related failures.
After using external resources (DB connections, open files, sockets), release them properly. Use fclose(), close database connections or finally blocks to ensure cleanup even if errors occur.
Each allocated resource consumes memory or system handles. Not releasing them can cause leaks or exhaustion (e.g., max DB connections). Proper cleanup ensures efficient operation and prevents resource‐related failures.
Side-by-side examples engineers can pattern-match during review.
<?php
$handle = fopen('file.txt', 'r');
// ... read file
// never close handle
?><?php
\$handle = fopen('file.txt', 'r');
try {
// ... read file
} finally {
if (\$handle) {
fclose(\$handle);
}
}
?><?php
$handle = fopen('file.txt', 'r');
// ... read file
// never close handle
?><?php
\$handle = fopen('file.txt', 'r');
try {
// ... read file
} finally {
if (\$handle) {
fclose(\$handle);
}
}
?>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.