Forum Discussion

Full Version: Prime Number Program in PHP w3schools
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Prime Number Program in PHP

gmp_nextprime— Find next prime number

PHP Code:
$var1 gmp_nextprime(9); // it will return next prime number greater than 9
$var2 gmp_nextprime(-999); // it will return next prime number greater than -999

echo gmp_strval($var1) . "<br>";
gmp_strval($var2) . "<br>"

gmp_prob_prime — Check if number is "probably prime".
The function uses Miller-Rabin's probabilistic test to check if a number is a prime.
It will 0 for not a prime number and 1 for probably a prime number and 2 for definitely a prime number.

PHP Code:
// It is definitely not a prime
echo gmp_prob_prime("6") . "<br>";

// It is probably a prime
echo gmp_prob_prime("1111111111111111111") . "<br>";

// It is definitely a prime
echo gmp_prob_prime("11") . "<br>"
Very well done. I will prefer using prime number program in PHP using for loop and prime number program in PHP using if else. It will simply check PHP program to check whether a number is prime or not and it will return a Boolean value of true. Here is the the simple function to check Prime numbers in PHP:

function isPrime($num)
if ($num < 2) {
return false;
for ($i = 2; $i <= $num / 2; $i++) {
if ($num % $i == 0) {
return false;

return true;
See some more functions that I found for program to find prime numbers in PHP