PHP - If number is divisible by 3 and 5 then echo
When starting out with PHP, it is common to want to keep logic tight while handling multiple conditions. Consider a scenario where you need to iterate through a list of numbers from 0 to 100 and label each one based on its divisibility: divisible by 3, divisible by 5, or divisible by both. The goal is to output the appropriate tag for each number, but leave it blank if it matches none of the criteria.
The core issue in the provided code lies in the logic used to check for numbers divisible by both 3 and 5. Specifically, this line attempts to solve that problem:
elseif ($number % 3 and 5 == 0) {
echo "BY3 AND 5";
}
This condition will not work as intended because of operator precedence and logical scope. In PHP, the and operator has a lower precedence than comparison operators like ==. More critically, the expression $number % 3 and 5 == 0 is parsed differently than one might expect. The parser evaluates $number % 3, then checks if the result is truthy, but the second part, 5 == 0, is a standalone comparison that is always false (since 5 does not equal 0). Consequently, this block will never execute.
To correctly identify numbers divisible by both 3 and 5, you must explicitly check the remainder for each divisor independently and combine them with the logical and operator or the bitwise &= operator, though the latter is less readable in this context. The corrected logic requires wrapping the modulo operations in parentheses to ensure they are evaluated before the boolean check:
elseif (($number % 3 == 0) && ($number % 5 == 0)) {
echo "BY3 AND 5";
}
Using && is generally preferred over and in control structures because && has higher precedence than assignment operators, which prevents subtle bugs when variables are assigned within the condition. However, in this specific case, simply grouping the comparisons ensures clarity and correctness.
There is another optimization available here using the Least Common Multiple (LCM). A number divisible by both 3 and 5 is, by definition, divisible by their LCM, which is 15. This mathematical property allows you to simplify the condition significantly:
elseif ($number % 15 == 0) {
echo "BY3 AND 5";
}
This approach reduces computational overhead slightly and makes the intent unmistakable: we are looking for multiples of 15. While the modulo check for both 3 and 5 is perfectly fine for readability, recognizing that n % 3 == 0 && n % 5 == 0 implies n % 15 == 0 is a valuable insight for performance-critical loops or large datasets.
Regarding the range generation, using range(0, 100) creates an array of integers from 0 to 100 inclusive. In modern PHP development, especially within frameworks like Laravel, you might find yourself generating sequences more often than not. While native PHP handles this well with arrays or loops, understanding how to manipulate these streams is fundamental before moving to higher-level abstractions. If your project eventually grows into a full-scale application using the Laravel framework, you will encounter helpers like range functions within collections that abstract this looping logic further, but mastering the underlying conditional logic remains essential for debugging and customization.
Here is the fully corrected and optimized version of the script:
<?php
$var = range(0, 100);
?>
<table>
<?php foreach ($var as $number) { ?>
<tr>
<td><?php echo $number; ?></td>
<td>
<?php
if ($number % 3 == 0) {
echo "BY3";
} elseif ($number % 5 == 0) {
echo "BY5";
} elseif ($number % 15 == 0) {
echo "BY3 AND 5";
}
?>
</td>
</tr>
<?php } ?>
</table>
Note that the order of checks matters. If you check for divisibility by 15 first, it will catch those numbers before checking for 3 or 5 individually. However, if the requirement is to display all applicable tags (e.g., a number divisible by 15 showing "BY3", "BY5", and "BY3 AND 5"), you would need to remove the elseif chain and use separate if statements or an array of conditions. For this specific request, assuming mutually exclusive labels for display purposes, the if/elseif structure works perfectly.