0% found this document useful (0 votes)
7 views1 page

15.write A PHP Script That Receives Form Input by The Method Post To Check The Number Is Prime or Not

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views1 page

15.write A PHP Script That Receives Form Input by The Method Post To Check The Number Is Prime or Not

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

15.

Write a PHP script that receives form input by the method post to check the number is
prime or not

<?php
// Function to check if a number is prime
function isPrime($num) {
if ($num <= 1) {
return false;
}
for ($i = 2; $i <= sqrt($num); $i++) {
if ($num % $i === 0) {
return false;
}
}
return true;
}
// Check if form is submitted
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Get the number from the form
$number = (int) $_POST['number'];

// Check if the number is prime


if (isPrime($number)) {
echo "The number $number is prime.";
} else {
echo "The number $number is not prime.";
}
}
?>

<html>
<head>
<title>Prime Number Checker</title>
</head>
<body>
<h1>Check Prime Number</h1>
<form method="post">
<label for="number">Enter a number:</label>
<input type="number" name="number" id="number" required>
<br>
<button type="submit">Check</button>
</form>
</body>
</html>

You might also like