Skip to content

All topics  /  PHP

How to Check Whether The Given Number Is Prime Or Not In PHP?

PHP ·

Teams applying “How to Check Whether The Given Number Is Prime Or Not In PHP?” in a production project can use learn more here to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.

Before copying the example into a live application, confirm version-specific behaviour with the PHP project and test it against the project’s actual database and runtime.

Hi Dev,

This tutorial is focused on how to check whether the given number is prime or not prime in PHP. you'll learn how to check whether the number is prime or not prime in PHP. We will use how to check whether the given number is prime or not prime using PHP. if you have a question about how to check whether the number is prime or not prime using PHP then I will give a simple example with a solution.

Now, let's see the article on how to check whether the given number is prime or not prime using PHP. it's a simple example of how to check whether the number is prime or not prime using PHP. you can understand the concept of how to check whether the number is prime or not prime in PHP. if you have a question about how to check whether the given number is prime or not prime in PHP then I will give a simple example with a solution.

Example 1: prime number program in PHP


<?php
    function IsPrimeOrNot($n){
        for($i=2; $i<$n; $i++){
            if($n % $i ==0){
                return 0;
            }
        }
        return 1;
    }
    // Pass The Number To 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";
    }
?>

Example 2: Prime Number using Form in PHP

<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>      

I hope it could help you...