Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
PHP program to check if all digits of a number divide it
To check if all digits of a number divide it in PHP, the code is as follows −
Example
<?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.
Advertisements