This is a basic PHP program which describes how to write Prime Number program in PHP. Below example will help you in the better understanding of Prime Number concept in PHP programming language.
We are using if…else statement and for loop to check whether the entered number is a Prime number or not.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<html> <head> <title>Prime number in PHP</title> </head> <body> <form method="post"> Enter a number: <input type="number" name="number"> <input type="submit" value="Submit"> </form> </body> </html> <?php /** * Prime number in PHP */ if ($_POST) { $form_data = $_POST['number']; for ($i = 2; $i <= $form_data - 1; $i++) { if ($form_data % $i == 0) { $value = True; } } if (isset($value) && $value) { echo 'The Number ' . $form_data . ' is not prime'; } else { echo 'The Number ' . $form_data . ' is prime'; } } |
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.