In this post, we will learn how to check if a number is prime or not prime in PHP. Before this, let us see what is prime or not prime number.

What is Prime or Not Prime Number ?

Prime Number : A number that can only be divided by itself is called Prime Number. For example 2, 3, 5, 7, 11, 13... These numbers can only be divided by itself.

Not Prime Number : A number that can be divided by itself or any other number is called Not Prime Number. For example 4, 6, 8, 10... These numbers are not prime numbers.

Check If A Number is Prime or Not Prime in PHP

To check if a number is prime or not prime in PHP, we follow the following code:

<?php
\$n = 2;
\$flag = 0;
for(\$i = 2; \$i < \$n; \$i++) {
if(\$n % \$i == 0) {
\$flag = 1;
break;
}
}

if(\$flag == 1) {
echo "Not Prime";
} else {
echo "Prime";
}
?> code-box

Output :
if \$n = 2, then output is Prime
if \$n = 4, then output is Not Prime

In this above we can also use \$i < (\$n/2) instead of \$i < \$n in for loop.

We can also use this above code as a function. In this code given below we make a function called checkPrime()

<?php
echo checkPrime(2);
function checkPrime(\$n) {
\$flag = 0;
for(\$i = 2; \$i < \$n; \$i++) {
if(\$n % \$i == 0) {
\$flag = 1;
break;
}
}

if(\$flag == 1) {
return "Not Prime";
} else {
return "Prime";
}
}
?> code-box

Output :
if checkPrime(2) then output is Prime
if checkPrime(4) then output is Not Prime

Check If A Number is Prime or Not Prime with Form

To check if a number is prime or not prime with form, we follow the following code. With this help of we can check multiple number dynamically.

<!DOCTYPE html>
<html>
<body>

<?php
if(isset(\$_POST['submit'])){
echo checkPrime(\$_POST['number']);
}

function checkPrime(\$n) {
\$flag = 0;
for(\$i = 2; \$i < \$n; \$i++) {
if(\$n % \$i == 0) {
\$flag = 1;
break;
}
}

if(\$flag == 1) {
return "Not Prime";
} else {
return "Prime";
}
}
?>

<form method="POST">
<input type="number" name="number" required/>
<input type="submit" name="submit" value="Submit"/>
</form>

</body>
</html> code-box

Output :
The output will be received according to the input value.

Get All Prime Numbers from a Range

To get all prime numbers form a range like 1 to 10, we follow the following code:

<!DOCTYPE html>
<html>
<body>

<?php
if(isset(\$_POST['submit'])){
for(\$i = 2; \$i <= \$_POST['number']; \$i++) {
\$result = checkPrime(\$i);
if(\$result == "Prime") {
}
}
//remove comma(,) from right side

function checkPrime(\$n) {
\$flag = 0;
for(\$i = 2; \$i < \$n; \$i++) {
if(\$n % \$i == 0) {
\$flag = 1;
break;
}
}

if(\$flag == 1) {
return "Not Prime";
} else {
return "Prime";
}
}
?>

<form method="POST">
<input type="number" name="number" required/>
<input type="submit" name="submit" value="Submit"/>
</form>

</body>
</html> code-box

Output :
if we input 10 then we get output as follows:
Prime Numbers : 2, 3, 5, 7

Summary

In this post we learn how to check if a number is prime or not prime in PHP. You try to make it yourself. I hope you make. If you face any problem, then comment and tell us. Keep visiting the blog to read more similar posts.