## Prime Number Program in PHP using while loop and for loop

We have been trying to give more and more **PHP programming examples for beginners **here and today we are doing so as well. Today we will be going to give you *PHP code examples* for Prime Numbers. So without further ado let’s start the **Prime number program in PHP using if else.**

## What is a Prime Number?

Before writing **program to find prime numbers in PHP **you have to now what are the Prime numbers or prime number definition. Here are three ways in which you can describe a prime number.

- A prime number is a natural number greater than 1 that cannot be formed by multiplying two smaller natural numbers.
- A number that is divisible only by itself and 1.
- A whole number that can
**not**be made by multiplying other smaller whole numbers.

Note: 2 is the only even number that is a prime or a prime number.

## How to find a Prime Number?

- How to find a whole number is a prime number or not, take a square root of the given number. Test whether the number is divisible by any prime number less than the square root. If yes, than the given number is not a prime number. If not, than the number is a prime number.
- Other method is the factorization method as to factor a number is to split that number down into smaller parts. To find out the prime factorization of a number, you require to break the number down to its prime factors.

Now as we know that what a prime number is and how to check a number if it is a prime number or not, let’s start writing the PHP function for Prime Numbers.

function isPrimeNumber($num) { //1 is not a prime number</code> if($num == 1){ return false; } //2 is prime (the only even number that is prime) if($num == 2){ return true; } // No even number is a prime number other than 2 if($num % 2 == 0) { return false; } /* Checks the odd numbers. If any of them is a factor, then it returns false. The sqrt can be an aproximation, hence just for the sake of security, one rounds it to the next highest integer value. */ $ceil = ceil(sqrt($num)); for($i = 3; $i <= $ceil; $i = $i + 2) { if($num % $i == 0) return false; } return true; }

Here is another way to check if a number is a prime number with while loop in php, **prime number program in PHP using while loop**:

function is_prime($number) { return !preg_match('/^1?$|^(11+?)\1+$/x', str_repeat('1', $number)); //using preg_match to find prime numbers } $start = 2; // start here (2 is the first prime) $end = 1000; $i=$start; while ($i<=$end) { if (is_prime($i)) echo $i." <br />\n"; $i++; }

Here is another function to find a prime using for loop in php:

function primeNumbers($n){ for($i=1;$i<=$n;$i++){ //numbers to be checked as prime $counter = 0; for($j=1;$j<=$i;$j++){ //all divisible factors if($i % $j==0){ $counter++; } } //prime requires 2 rules ( divisible by 1 and divisible by itself) if($counter==2){ print $i." is Prime <br/>"; } } }

This is another very simple and easy way of finding Prime Numbers in PHP:

function isPrimeNumber($i) { $n = 2; while ($n < $i) { if ($i % $n) { $n++; continue; } return false; } return true; }

I hope you would have liked the post. We try to be precise in writing and sharing code. If anything is unclear to you please do comment and we will give you reply. For further PHP examples with source code please keep on visiting us.

Similar post Factorial Program in PHP Using for Loop and While Loop