In PHP, you can easily output all numbers divisible by 4 within a specified range by combining a loop with a condition check. Using a for loop to iterate through each number in the range and checking if it’s divisible by 4, the matching numbers will be printed.
Here is an example that accomplishes this in one line:
<span class="fun">$start = 1; $end = 10; for ($i = $start; $i <= $end; $i++) { if ($i % 4 == 0) echo $i . ' '; }</span>
In this code, $start and $end define the range limits. The for loop iterates through each integer in this range, and the condition $i % 4 == 0 checks whether the number is divisible by 4. If true, the number is output.
You can save the above code into a PHP file and run it on a server that supports PHP by following these steps:
Create a new file, for example, output_numbers.php, and paste the code inside;
Make sure the file extension is .php and place it in the proper directory on your server;
Access the file in your browser by entering its URL, such as http://localhost/output_numbers.php;
The page will then display all numbers divisible by 4 within the specified range.
<span class="fun">4 8 </span>
For the range from 1 to 10, the output shows 4 and 8, which confirms the code correctly filters and displays the matching numbers.
By combining a simple for loop with a condition, you can efficiently output all numbers divisible by a certain value within any range. Adjusting the $start and $end variables allows flexible use for different intervals. This approach is ideal for beginners to understand loops and the modulo operator while solving similar problems quickly.