PHP Prime Number Program | PHP Tutorial

Prime number program in PHP. Through this tutorial, we would love to share with you how to check whether a given number is prime or not.
In this tutorial, we will create a PHP function to check a given number is prime or not. And we will create a new PHP program with PHP form, that also checks a number is prime or not
What is Prime Number?
Answer: A number that is only divisible by 1 and itself is called a prime number. The first few prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23 and 29.
prime number program in PHP
We will create a program in PHP that can check whether a given number is prime or not.
<?php
function IsPrimeOrNot($n)
{
for($i=2; $i<$n; $i++)
{
if($n % $i ==0)
{
return 0;
}
}
return 1;
}
// Pass The Number For check it is prime or not
$a = IsPrimeOrNot(5);
if ($a==0)
echo 'This is not a Prime Number.....'."\n";
else
echo 'This is a Prime Number..'."\n";
?>
Prime Number using Form in PHP
Here we will create PHP program with PHP Forms for how to check a whether a given number is prime or not.
<html>
<head>
<title>Prime Number using Form in PHP</title>
</head>
<body>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
Enter a Number: <input type="text" name="input"><br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if($_POST)
{
$input=$_POST['input'];
for ($i = 2; $i <= $input-1; $i++) {
if ($input % $i == 0) {
$value= True;
}
}
if (isset($value) && $value) {
echo 'The Number '. $input . ' is not prime';
} else {
echo 'The Number '. $input . ' is prime';
}
}
?>
</body>
</html>
Recommended PHP Tutorials
Recommended:-jQuery Ajax Form Submit with FormData Example
Recommended:-jQuery Form Validation in PHP
Recommended:-PHP 8 Pagination with Bootstrap Example
Recommended:-User Registration with Email Verification in PHP
Recommended:-How to Add Captcha in PHP Registration Form
Recommended:-PHP Contact Form with jQuery Validation Example
Recommended:-PHP Cookie Set, Get And Delete Example
Recommended:-Insert Update Delete in PHP on Same Page
Recommended:-How to Fetch Data From Database in PHP using Ajax
Recommended:-File Upload in PHP MySQL database
Recommended:-PHP | Implode and Explode Function
Recommended:-Send Reset Password Link Email PHP
Recommended:-Crop and Save Image Using jQuery Croppie and PHP
Recommended:-PHP Display All Errors Example
Recommended:-PHP Google Places Autocomplete Example
Recommended:-PHP Move and Copy File From One Folder to Another
Recommended:-PHP Dropzone File Upload Tutorial Example
Recommended:-PHP Signature Pad using Jquery Ajax Example
Recommended:-PHP 8 MySQL Ajax Live Search Autocomplete Example
Recommended:-PHP Google Firebase CRUD Example Tutorial
Recommended:-Replace First and Last Character From String PHP
Recommended:-How to Install PHP 8 on Ubuntu 20.04 using Terminal
Recommended:-PHP Sending Email (text/html)|PHP Mail() Function
Recommended:-Import CSV File into MySQL using PHP
Recommended:-Upload File to AWS S3 Bucket In PHP
Recommended:-Login with Facebook using PHP
Recommended:-PHP MySQL jQuery Ajax Pagination
Recommended:-PHP and MySQL DataTables Server-side Processing
Recommended:-Datatables CRUD with Ajax, PHP & MySQL
Recommended:-PHP Environment Variables Windows 10 Xampp
Recommended:-PHP Add Days, Month, Year to Date
Recommended:-PHP Subtract Days, Month, Year to Date
Recommended:-PHP Import JSON File data into MySQL database
Recommended:-PHP Xampp Path Environment Variable Windows 10/11



