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 Append a String in PHP How To Prepend a String in PHP Replace a Word In String in PHP Convert Special Characters To HTML Entities PHP Find Number of Words in a String in PHP How To Find String Length in PHP How To Convert String To Uppercase in PHP How To Convert String To Lowercase in PHP How to Combine Two Strings in PHP Delete Array Elements By Value in PHP Get First Element Of Array In PHP Remove Numbers From String in PHP Convert Timestamp To Date in PHP Convert a Date to Timestamp in PHP Get Current Page Slug in PHP Check If Session Is Started in PHP Split Sentence Into Words in PHP Remove Last Character From String in PHP PHP Remove Spaces From String Replace Space With Dash in PHP Remove Everything from String Except Numbers Using PHP Get Full Url Address Using PHP Automatic Copyright Year Using PHP Generate 4 Digit Random Number in PHP How To Check If a Number is Prime using PHP Convert Integer to String in PHP