Home > Article > Backend Development > 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!