Published on 14 October 2019 (Updated: 14 October 2019)

Welcome to the Prime Number in Php page! Here, you'll find the source code for this program as well as a description of how the program works.

Current Solution

<?php/**
* Function to determine if a number if prime.
* @param int positive integer.
* @return True if the number is prime, False otherwise.
*/functionis_prime($num){if(($num%2==0&&$num!=2)||($num==1)){returnfalse;}$found_factor=false;for($n=3;$n<=intval(ceil(sqrt($num)));++$n){if(($num%$n)==0){$found_factor=true;break;}}return!$found_factor;}// Check argumentif($argc<2||!is_numeric($argv[1])||strpos($argv[1],'.')!==false||strpos($argv[1],'-')!==false){echo"Usage: please input a non-negative integer\n";exit(1);}// Convert the stringif(is_prime(intval($argv[1]))){echo"Prime\n";}else{echo"Composite\n";}exit(0);

Note: The solution shown above is the current solution in the Sample Programs repository as of May 09 2022 21:44:19. The solution was first committed on Oct 14 2019 15:37:04. As a result, documentation below may be outdated.