PHP program to check if all digits of a number divide it

PHPServer Side ProgrammingProgramming

PHP Online Training

45 Lectures 9 hours

Learn PHP from Scratch

17 Lectures 1 hours

PHP in Telugu

43 Lectures 5.5 hours

To check if all digits of a number divide it in PHP, the code is as follows −

Example

Live Demo

<?php
function divisibility_check($my_val,$my_digit)
{
return ($my_digit != 0 &&$my_val % $my_digit == 0); } function divide_digits($n)
{
$temp =$my_val;
while ($temp > 0) {$my_digit = $my_val % 10; if (!(divisibility_check($my_val, $my_digit))) return false;$temp /= 10;
}
return true;
}
$val = 255; if (divide_digits($val))
echo "All the numbers can be divided";
else
echo "All the numbers can't be divided";
?>

Output

All the numbers can be divided

A function named ‘divisibility_check’ is defined that checks to see if the number is not 0 and if the number completely divides every digit of the number, without leaving any value as a remainder. Another function named ‘divide_digits’ is defined that checks to see if every digit in the number divides the number completely. The number is defined, and the ‘divide_digits’ function is called by passing this number as the parameter. Relevant message is displayed on the console.

Updated on 02-Jul-2020 07:00:08