Why this matters
Using goto can create spaghetti code that’s hard to follow and prone to errors. Conventional control structures make code more readable and easier to debug.
Avoid using the goto statement for flow control. Instead, refactor code using loops (for, while) and conditional structures (if/else, switch) for clear and maintainable logic.
Using goto can create spaghetti code that’s hard to follow and prone to errors. Conventional control structures make code more readable and easier to debug.
Side-by-side examples engineers can pattern-match during review.
<?php
// Bad goto usage
if (!validate(\$data)) {
goto error;
}
// ... other operations ...
error:
echo "An error occurred";
?><?php
if (!validate(\$data)) {
echo \'An error occurred';
} else {
// ... other operations ...
}
?><?php
if (!validate(\$data)) {
echo "An error occurred";
} else {
// ... other operations ...
}
?>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.