Forum Discussion
PHP Function to Check Prime Number - Printable Version

+- Forum Discussion (http://forum-discussion.com)
+-- Forum: Coding Problems (http://forum-discussion.com/Forum-Coding-Problems)
+--- Forum: PHP (http://forum-discussion.com/Forum-PHP)
+--- Thread: PHP Function to Check Prime Number (/Thread-PHP-Function-to-Check-Prime-Number)



PHP Function to Check Prime Number - Kelly - 08-19-2017

Here is the best prime number program in PHP using for loop. It is quite simple and easy PHP function to check prime numbers. PHP prime number generator function.
PHP Code:
function is_prime($number){
 
   // 1 is not prime
 
   if $number == ) {
 
       return false;
 
   }
 
   // 2 is the only even prime number
 
   if $number == ) {
 
       return true;
 
   }
 
   // square root algorithm speeds up testing of bigger prime numbers
 
   $x sqrt($number);
 echo 
$x."==x<br>";
 
   $x floor($x);
 
   for $i $i <= $x ; ++$i ) {
 
       if $number $i == ) {
 
           break;
 
       }
 
   }
 
   
    if
$x == $i-) {
 
       return true;
 
   } else {
 
       return false;
 
   }