Check if a number is divisible by another number in PHP

Check if a number is divisible by another number in PHP

In this post we will give you information about Check if a number is divisible by another number in PHP. Hear we will give you detail about Check if a number is divisible by another number in PHPAnd how to use it also give you demo for it if it is necessary.

we are going to learn about how to check if a number is divisible by another number in PHP with the help of examples.

Consider, we have a following number:

$a = 20

To find if a above number 20 is divisible by another number, we should divide the first number by second and get the remainder 0 then only we call it’s a divisible of another number otherwise it is not a divisible.

For example: 20%5 = 0

Using % Modulo operator

To check if a number is divisible by another number, we can use the % modulo operator in PHP.

The modulo % operator returns the remainder of two numbers 20 % 5 = 0, so if we get a remainder 0 then the given number is a divisible of another number otherwise it not a divisible.

Here is an example:

if (20 % 5 == 0) {  echo "20 is divisible by 5";}else {  echo "20 is not divisible by 5";}

Output:

"20 is divisible by 5"

In the above code we have added 20 % 5 == 0 in if condition. so 20 is divided by 5 and returns the remainder 0 then it prints the output “20 is divisible by 5”.

Example 2 :

if (100 % 10 == 0) {  echo "100 is divisible by 10";}else {  echo "100 is not divisible by 10";}

Output:

"100 is divisible by 10"

Checking if a number is not divisible by another

To check if a number is not a divisible of another number, we can use the modulo operator % but the remainder of first number by second number is not equal to 0.

Here is an example:

if (25 % 10 != 0){    echo "25 is not divisible by 10";}

In the above code, 25 is divided by 10 and returns the remainder 5. So the given number is not divisible of 10.

Hope this code and post will helped you for implement Check if a number is divisible by another number in PHP. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US