This post contains a total of 5+ Hand-Picked PHP Prime number checker Program Examples with Source Code. All these prime number checker programs are made using PHP Programming Language.
You can use the source code of these examples with credits to the original owner.
Related Posts
PHP Prime Number Checker Programs
1. By alireza karampoor
Made by alireza karampoor. Basic PHP Program to check if input number is prime number. Source
not a prime num
<?php
//change the value of $x
$x=21;
for($y=2;$y<$x;$y++){
$z=$x%$y;
if($z==0){
echo"not a prime num";
break;
}elseif($z!=0){
if($y==$x-1){
echo "prime ! ";
break;
}else{
continue;
}
}
}
?>
2. By Rishabh(CP)
Made by Rishabh(CP). Simple Prime number Checker Program, enter your input in checkPrime(7). Source
7 Prime number 8 Not a prime
<?php
function engine($val){
for($n = 2; $n<= $val/2;$n++){
if($val % $n == 0){
return 0;
}
}
return 1;
}
function checkPrime($num){
$flag = engine($num);
if($flag == 1){
echo "Prime number </br>";
}
else{
echo "Not a prime </br>";
}
}
checkPrime(7);
checkPrime(8);
?>
3. By Harsh
Made by Harsh. This program prints out all the prime number in a given range. Source
2is a prime 3is a prime 5is a prime 7is a prime
<?php
for($a=2;$a<10;$a++)
{
for($b=2;$b<=$a;$b++)
{
if($a%$b==0)
break;
}
if($a==$b)
echo $a."is a prime <br />";
}
?>
4. By Ankur jadhav
Made by Ankur jadhav. You need to enter your input number in $a = IsPrime(52) to check if its prime number. Source
This is a Prime Number..
<?php
function IsPrime($n)
{
for($x=2; $x<$n; $x++)
{
if($n %$x ==0)
{
return 0;
}
}
return 1;
}
$a = IsPrime(3);
if ($a==0){
echo 'This is not a Prime Number.....';
}
else{
echo 'This is a Prime Number..'."\n";
}
?>
5. By Quang Hoang
Made by Quang Hoang. Program to check and print the amount of prime numbers you need from 0 to infinity, enter the limit in echo “songuyento(5);” . Source
2 3 5 7 11
<?php
function kiemtra($a){
$ck = 1;
for ($i =2; $i<= sqrt($a); $i++){
if($a % $i==0 && $a > 2 ){
$ck = 0;
}
}
return $ck;
}
function songuyento($n){
$i = 2;
$temp = 0;
(string)$result = "";
while ($temp < $n){
if(kiemtra($i)==1){
$result .= $i . " ";
$temp ++;
}
$i++;
}
return $result;
}
echo songuyento(5);
?>
6. By Yashraj Singh
Made by Yashraj Singh. Program to check if a number is prime number or composite. Source
2 is Prime Number
<html>
<body>
<?php
//Prime Number Check
$n=2;
$flag=0;
for($i=2;$i<11;$i++)
{
if($n%$i==0)
$flag=1;
}
if($flag==0)
echo $n." is Prime Number ";
else
echo $n." is Composite Number ";
?>
</body>
</html>