How To Check If a Number is Prime using PHP

In PHP we can easily Check If a Number is Prime or not. Five simple examples of PHP program are given below to check if given number is prime or not.

What is Prime Number

Prime number is a number which only has two factors, 1 and the number itself.

Example 1

In this example a Modulo operator is used to check the remainder of 10 divided by all the numbers from 1 to 10. If remainder comes 0 more than 3 times then it means the number (10 in this example) is not a prime number.

<?php
$number = 10;  
$count=0;  
for ( $i=1; $i <= $number; $i++)  
{  
if (($number % $i)==0)  
{  
$count++;  
}  
}  
if ($count < 3)  
{  
echo "$number is a prime number.";  
}
else
{
echo "$number is not a prime number."; 
}
?>

Output

10 is not a prime number.

Example 2

This example also works on same principle, but in this example a function is created to check the if number is prime or not.

<?php
function PrimeNumber($n)
{
for($a=2; $a < $n; $a++)
{
if($n % $a == 0)
{
return 0;
}
}
return 1;
}
$x = PrimeNumber(5);
if ($x == 0)
{
echo "This is not a Prime Number.";
}
else
{
echo "This is a Prime Number.";
}
?>

Output

This is a Prime Number.

Example 3

gmp_prob_prime() function can also be used to check if a number is prime or not. If the function returns 0 it means that the number is not prime, if it returns 1 then number is probably prime, but if it returns 2 then number is surely a prime number.

<?php
echo gmp_prob_prime("6");
?>

Output

0

Example 4

In this example a number is divided by 2 to n/2 and remainder is checked every time, if the number divides completely then 0 will be returned, meaning that the number given is not a prime number.

<?php
function primeCheck($y){
$num = $y;
if ($num == 1)
{
return 0;
}
for ($i = 2; $i <= $num/2; $i++){ 
if ($num % $i == 0) 
return 0; 
} 
return 1; 
} 
$num = 27; 
$x = primeCheck($num); 
if ($x == 1) 
{
echo "Prime Number";
}
else
{
echo "Not a Prime Number";
}
?>

Output

Not a Prime Number

Example 5

The following example is suitable for larger numbers since it's much more efficient. The example checks only till square root of n instead of n, Since if given number is divisible by square root of n then it must be divisible by the number n.

<?php
function primeNumber($number){ 
if ($number == 1)
{
return 0; 
}
else
{
for ($i = 2; $i <= sqrt($number); $i++){ 
if ($number % $i == 0) 
return 0; 
}
return 1; 
}
} 

$num = 27; 
$x = primeNumber($num); 
if ($x == 1) 
{
echo "Prime Number"; 
}
else
{
echo "Not a Prime Number";
}
?>

Output

Not a Prime Number

How To Remove Empty Array Elements with PHP How To Remove Common Values from two Arrays with PHP How To Find sum of array values with PHP How To Shuffle array Values in PHP How to Remove Duplicate Values from an array with PHP PHP Sort Associative array by Key PHP Sort Associative array by Value Sort array in numerical order with PHP Sort array in alphabetical order with PHP Sort array in descending order PHP Sort array in ascending order in PHP How to merge two or more arrays in PHP Add elements to the end of an array in PHP Add elements to the start of an array in PHP PHP Remove First Element from an Array PHP Remove Last Element from an Array Check if Key Exists in array PHP Check if Value exists in array PHP How to reverse an array in PHP PHP Print array line by line PHP Echo array values using foreach loop Count array values in PHP PHP Count How Many Times Word appears in String How to Reverse a String with PHP Check if Variable is Null in PHP