Prime Number or not using PHP

<?php

if ($_SERVER[ "REQUEST_METHOD "] == "POST ") {
$num = $_POST[ "num "];

$is_prime = true;

if ($num <= 1) {
$is_prime = false;
} else {
for ($i = 2; $i <= sqrt($num); $i++) {
if ($num % $i == 0) {
$is_prime = false;
break;
}
}
}

if ($is_prime) {
echo "$num is a prime number! ";
} else {
echo "$num is not a prime number. ";
}
}
?>

<form method= "post ">
<label for= "num ">Enter a number:</label>
<input type= "number " id= "num " name= "num ">
<input type= "submit " value= "Check ">
</form>

 

 

This code first checks if the form is submitted using the $_SERVER[“REQUEST_METHOD”] variable. If the form is submitted, it gets the number entered in the input box using $_POST[“num”].

Then it sets a boolean variable called $is_prime to true. If the number is less than or equal to 1, it is not a prime number, so $is_prime is set to false.

If the number is greater than 1, it loops through all numbers from 2 up to the square root of the number.

If any of those numbers divide the original number evenly, it is not a prime number, so $is_prime is set to false and the loop is broken.

if $is_prime is still true, the number is a prime number, so the code displays a message saying so. Otherwise, it displays a message saying that the number is not a prime number.