How to use array_filter() and range() Functions In PHP

Another approach involves using array_filter() along with range() to create an array of numbers and filter out those divisible by both 3 and 5.

PHP




<?php
  
function check3And5Divisible($number) {
    return ($number % 3 == 0 && $number % 5 == 0);
}
  
function printDivisibleNumbers($num) {
    $numbers = range(1, $num);
    $divisibleNumbers
        array_filter($numbers, 'check3And5Divisible');
          
    echo implode(' ', $divisibleNumbers);
}
  
// Driver code
$num = 50;
  
printDivisibleNumbers($num);
  
?>


Output

15 30 45


Print All Numbers Divisible by 3 and 5 for a Given Number in PHP

This article will show you how to print all the numbers less than N, that are divisible by 3 and 5 in PHP. When working with numbers, it’s common to need a program that can identify and print all numbers divisible by specific values. In this article, we will explore different approaches in PHP to print all numbers divisible by 3 and 5 up to a given number.

Table of Content

  • Using for Loop
  • Using array_filter() and range() Functions

Similar Reads

Using for Loop

The basic method to achieve this is by using a loop to iterate through numbers up to the given limit and checking divisibility....

Using array_filter() and range() Functions

...

Contact Us