PHP Prime Number Program
Definition
What Is a Prime Number?
A prime number is an integer greater than 1 that has exactly two positive divisors: 1 and itself.
Prime: 2, 3, 5, 7, 11, 13, 17, 19, ...
Not prime: 1 (neither), 4, 6, 8, 9, 10, 12, ...
Rule:
n > 1 and no divisor in 2 .. floor(√n)
| n | Divisors | Prime? |
|---|
7 | 1, 7 | Yes |
9 | 1, 3, 9 | No |
2 | 1, 2 | Yes (only even prime) |
1 | 1 | No |
Approach
How to Check a Prime Number in PHP
Four clear steps — then reuse the helper to list primes in a range.
- If
n ≤ 1, return false. - If
n is 2 or 3, return true (optional fast path). - Try divisors from
2 to √n; if any divides n, it is not prime. - If no divisor is found,
n is prime.
| Method | Idea | Time | Best for |
|---|
| Trial to √n | No divisor in 2..√n | O(√n) | Single check (default) |
| Range scan | Call isPrime for each value | O(N√N) | Listing primes 1..N |
Pseudocode
function IsPrime(n):
if n <= 1: return false
i = 2
while i * i <= n:
if n % i == 0:
return false
i = i + 1
return true
Reference
Cheat Sheet
Keep these facts handy while writing the program.
| Topic | Detail |
|---|
| Definition | n > 1 with only divisors 1 and n |
| 1 | Neither prime nor composite |
| 2 | Smallest and only even prime |
| Loop bound | i * i <= n (same as i <= √n) |
| Composite | Has a factor ≤ √n |
| Classic checks | 7 yes; 9 no; 1 no |
Try it
Live Preview
Enter an integer to see trial division up to √n and the prime verdict.
Trace
Worked Walkthrough — n = 17
Test every candidate divisor up to √17.
√17 ≈ 4.123 → check i = 2, 3, 4
17 % 2 = 1
17 % 3 = 2
17 % 4 = 1
No divisor → 17 is prime
Counterexample n = 15:
√15 ≈ 3.87 → check i = 2, 3
15 % 3 == 0 → not prime
Code
PHP Programs
Start with a reusable isPrime helper. Then print every prime in a small range with the same helper.
Example 1 — isPrime Check
Return true only when no divisor exists from 2 through √n.
<?php
function isPrime(int $n): bool
{
if ($n <= 1) {
return false;
}
for ($i = 2; $i * $i <= $n; $i++) {
if ($n % $i === 0) {
return false;
}
}
return true;
}
$number = 17;
if (isPrime($number)) {
echo "$number is prime\n";
} else {
echo "$number is not prime\n";
}
How It Works
$n <= 1 is rejected — primes start at 2.- The loop runs while
$i * $i <= $n, which means $i ≤ √n. - If any
$i divides $n, a third divisor exists → not prime. - If the loop finishes with no hit, only divisors are 1 and
$n. - For
17, checks stop at 4 and none divide, so the answer is true.
Example 2 — Primes in a Range
Reuse isPrime to print every prime from 1 to 20.
<?php
function isPrime(int $n): bool
{
if ($n <= 1) {
return false;
}
for ($i = 2; $i * $i <= $n; $i++) {
if ($n % $i === 0) {
return false;
}
}
return true;
}
$start = 1;
$end = 20;
echo "Primes from $start to $end:\n";
for ($i = $start; $i <= $end; $i++) {
if (isPrime($i)) {
echo "$i ";
}
}
echo "\n";
Primes from 1 to 20:
2 3 5 7 11 13 17 19
How It Works
- Same
isPrime helper as Example 1 keeps the logic in one place. - The outer loop visits every integer from
$start to $end. - Only values that pass
isPrime are printed. 1 is skipped automatically by the helper.- For larger ranges, a sieve is faster — but this pattern is perfect for beginners.
Edge Cases & Pitfalls
Check these before calling the solution done.
1Not prime
Return false for $n ≤ 1 before looping.
2Even but prime
Do not reject all even numbers — 2 is prime.
BoundUse i * i ≤ n
Avoid floating sqrt in the loop condition when possible.
NegativesNot prime here
This tutorial defines primes on positive integers only.
Analysis
Time and Space Complexity
| Program | Time | Extra space |
|---|
| isPrime (√n trial) | O(√n) | O(1) |
| Primes in range 1..N | O(N√N) | O(1) |
For large ranges, the Sieve of Eratosthenes is faster. For one number or a short range, √n trial division is enough.
Remember
Key Takeaways
Definition: n > 1 with only divisors 1 and n.
Check: no divisor from 2 to √n.
Facts: 1 is not prime; 2 is the only even prime.
Test set: 17 → yes; 15 → no; 1 → no; 2 → yes.
One line: if $n > 1 and nothing from 2 to √n divides it, $n is prime.
Frequently Asked Questions
A prime number is an integer greater than 1 with exactly two positive divisors: 1 and itself. Examples: 2, 3, 5, 7, 11.
No. 1 has only one positive divisor, so it is neither prime nor composite.
If n has a factor larger than √n, the matching factor is already smaller than √n and would have been found earlier.
Yes. 2 is the only even prime.
A single isPrime check with trial division up to √n is O(√n). Listing primes from 1 to N by calling isPrime on each value is about O(N√N).
CODETOFUN NEWS
Stay Ahead in Tech
Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
10 people found this page helpful