Home >Backend Development >PHP Problem >PHP determines whether a prime number is

PHP determines whether a prime number is

(*-*)浩
(*-*)浩Original
2019-10-19 11:15:443555browse

PHP determines whether a prime number is

Determine whether it is a prime number. A prime number refers to a natural number that has no other factors except 1 and itself among natural numbers greater than 1. (Recommended learning: PHP video tutorial)

<html> 
<?php
 
if (!empty($_POST)) {
    $num = $_POST["num"];
    $count = 0;
    for ($i = 1; $i <= $num; ++$i) {
        if ($num % $i == 0) {
            $count++;
        }
    }
} else {
    $num = "";
    $count = "";
}
 
?>
 
<form action="" method="post">
    <input type="text" name="num" value="<?php
    echo $num;
    ?>"/>
    <input type="button" name="tip" value="<?php
    if ($count == 2) {
        echo $num . "是素数";
    } else {
        echo $num . "不是素数";
    }
    ?>"/>
    <input type="submit" value="判断"/>
</form>
 
 
</html>

The above is the detailed content of PHP determines whether a prime number is. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn