A prime number is a natural number greater than 1 that is not a product of two smaller natural numbers.
Such as – 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43…
See the below code how to find or get all prime number in between 1 to 100 –
<?php
for($i=1;$i<=100;$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/>";
}
}
Explanation :
Here i’m taking two ‘for loop’.First ‘for loop’ for 100 numbers, then i take ‘$counter’ variable with value 0(zero).Then i start second ‘for loop’ which always start from 1 and run till first ‘for loop’ value, after that i did one ‘if’ condition where i modulo ‘first for loop’ value to ‘second for loop’ value and check is there any remainder exist or not, if no remainder then i increase the ‘$counter’ value by 1, and finally i check one more condition that above ‘$counter’ value is equal to ‘2’ or not,if it’s equal to ‘2’ then this number is prime number.