PHP Prime Number: Check Prime Number with Examples

Introduction

In PHP, checking whether a number is prime or not is a popular beginner-level as well as interview question. It helps developers understand loops, conditions, and optimization techniques.

In this article, you will learn what a prime number is and how to implement prime number logic in PHP using different methods.

What is a Prime Number?

A prime number is a natural number greater than 1 that has only two factors:

  1. 1
  2. The number itself

In simple terms:
A number is prime if it is divisible only by 1 and itself.

Examples:

2, 3, 5, 7, 11 → Prime numbers 

4, 6, 8, 9 → Not prime numbers

📖
Important Points:
  • 1 is not a prime number
  • 2 is the smallest prime number
  • Prime numbers are always greater than 1

Syntax (General Logic)

To check whether a number is prime:

  1. Take a number
  2. Check if it is divisible by any number other than 1 and itself
  3. If divisible → Not Prime
  4. If not divisible → Prime

Examples

Example 1: Basic Prime Number Program


<?php
$num = 7;
$isPrime = true;
if ($num <= 1) {
   $isPrime = false;
} else {
   for ($i = 2; $i < $num; $i++) {
       if ($num % $i == 0) {
           $isPrime = false;
           break;
       }
   }
}
if ($isPrime) {
   echo "$num is a Prime Number";
} else {
   echo "$num is not a Prime Number";
}
?>

Example 2: Optimized Prime Check


<?php
$num = 29;
$isPrime = true;
if ($num <= 1) {
   $isPrime = false;
} else {
   for ($i = 2; $i <= sqrt($num); $i++) {
       if ($num % $i == 0) {
           $isPrime = false;
           break;
       }
   }
}
echo $isPrime ? "Prime Number" : "Not Prime Number";
?>

Note: This is more efficient because it checks only up to √n.

Example 3: Function to Check Prime


<?php
function isPrime($num) {
   if ($num <= 1) return false;
   for ($i = 2; $i <= sqrt($num); $i++) {
       if ($num % $i == 0) return false;
   }
   return true;
}
echo isPrime(11) ? "Prime" : "Not Prime";
?>

Example 4: Print Prime Numbers up to N


<?php
$n = 20;
for ($num = 2; $num <= $n; $num++) {
   $isPrime = true;
   for ($i = 2; $i <= sqrt($num); $i++) {
       if ($num % $i == 0) {
           $isPrime = false;
           break;
       }
   }
   if ($isPrime) {
       echo $num . "<br/>";
   }
}
?>

Example 5: Using a While Loop


<?php
$num = 10;
$i = 2;
$isPrime = true;
while ($i <= sqrt($num)) {
   if ($num % $i == 0) {
       $isPrime = false;
       break;
   }
   $i++;
}
echo $isPrime ? "Prime" : "Not Prime";
?>

Real-Life Example

Scenario 1 : Password Security

Prime numbers are used in encryption algorithms to secure user passwords.

Scenario 2: Number Filtering System


<?php
$numbers = [2, 4, 7, 9, 11];
foreach ($numbers as $num) {
   if (isPrime($num)) {
       echo $num . " is Prime<br/>";
   }
}
?>

👉 Useful in filtering datasets.

Common Mistakes

1. Considering 1 as Prime


if($num == 1)

👉 1 is not a prime number.

2. Wrong Loop Range


for($i = 1; $i <= $num; $i++)

👉 Should start from 2.

3. Not Breaking Loop

Missing break increases unnecessary iterations.

4. Ignoring Optimization

Checking till $num instead of sqrt($num).

5. Negative Numbers

Prime numbers are only positive integers greater than 1.

Conclusion

The prime number program in PHP is a fundamental concept that helps you understand loops, conditions, and optimization techniques. It is widely used in mathematical computations, security systems, and programming interviews.

By mastering prime numbers, you can:

  • Improve logical thinking
  • Write optimized code
  • Solve complex problems