You are here: Home / Topics / How to check prime number in PHP?

How to check prime number in PHP?

Filed under: PHP on 2022-11-16 13:34:57

The below program checks if a number is a prime or a composite number. The PHP echo statement is used to output the result on the screen.
Example

<!DOCTYPE html>
<html>
<body>

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

</body>
</html>
 

Output:

13 is a prime number.

About Author:
V
Vishal Gupta     View Profile
Hi, I am Vishal Gupta. I like this Website for learning MCQs. This website is good for preparation.