If you are looking to **check a number is prime in PHP** then you are at the right place because here we will show whether a number is prime or not using PHP.

Here I am just showing you a** program to check prime number in php**. The technique that I am going to show you very simple and simple but quite potent and here you will also find how to alter and personalize these codes readily according to your need.

A Number that is divisible by itself as well as 1 then it called given number is Prime Number. For eg- in the event that you take the number 11, it can only be divided to acquire a complete number if it is divided by 1 or 11. If any other number is used subsequently a percentage is obviously found.

<?php if(isset($_POST['submit'])) { //retrieve number form POST submission $num=$_POST['num']; $primeFlag=0; //if not perfectly divisible by any, //number is prime for($i=2;$i<$num;$i++) { $primeFlag=0; if(($num%$i)==0) { break; } $primeFlag=1; } if($primeFlag==1) { echo " $num is Prime Number"; } else { echo "$num is not Prime number"; } } ?> <!DOCTYPE html?> <html?> <head> <title>Write a Program to check Prime Number</title> </head> <body> <form method="post"> <table> <tr> <td>Enter Your Number</td> <td><input type="text" name="num"></td> </tr> <tr> <td colspan="2" align="center"> <input type="submit" name="submit" value="submit"> </td> </tr> </table> </form> </body> </html>

Output:

<?php if(isset($_POST['submit'])) { //retrieve number form POST submission //convert to array by splittiing on comma $numStr=$_POST['num']; $numArr=explode(",",$_POST['num']); $primes=array(); $primeFlag=0; //iterate over array //get absolute values of each number foreach($numArr as $n) { $n=trim(abs($n)); //test each number for prime-ness: //check the number by dividing it //by all the numbers between 2 and itself //if not perfectly divisible by any, //number is prime for($i=2;$i<$n;$i++) { $primeFlag=0; if(($n%$i)==0) { break; } $primeFlag=1; } //if prime add to output array if($primeFlag==1) { array_push($primes,$n); } } //check if any primes number were found then sort and remove duplicate from array and prit if(count($primes)>0) { $primes=array_unique($primes); sort($primes); echo "The following numbers are prime : ".implode($primes," "); } else { echo "No Prime numbers found"; } } ?> <!DOCTYPE html> <html> <head> <title>check prime number</title> </head> <body> <form method="post"> <table> <caption><h3>Project : Prime Number Tester</h3></caption> <tr> <td>Enter a list of numbers, Seperated by Commas</td> <td><input type="text" name="num"></td> </tr> <tr> <td colspan="2" align="center"> <input type="submit" name="submit" value="submit"> </td> </tr> </table> </form> </body> </html>

Total Downloads : 3

Login / Register To Download
PHP Reference
Find Minimum value of an array
PHP get max value in array
Find second largest number in array
PHP get highest value in multidimensional array
Sort an array in PHP
Sort an array in descending order in PHP
Age calculator in PHP
How to Get the Current Page URL
Convert Number into Words in PHP
Simple Steps to Fix 403 Forbidden Errors
How To Change Port of Apache
Get Multiple checkbox value in PHP
How to Print January to December using PHP
PHP Read Pdf file
How to remove duplicate characters from string
How to reverse words in a string
How to Get Difference between two dates
Sum of two numbers without using + operator in PHP
Check A Number is Prime or Not in PHP