Odd Number Generator in PHP

June 6, 2020
PHP
odd number generator in php

In this tutorial, we will create an Odd Numbers Generator using PHP. This code will instantly separate all the possible odd numbers on the web page. This code uses a PHP POST method to dynamically generate an odd number in an array of numbers using a conditional algebraic statement. This is a user-friendly kind of program feel free to modify it.

We will be using PHP as a scripting language that manages a database server to handle a bulk of data per transaction. It describes as an advanced technology that manages both server and control-block of your machine.

Getting Started:

First, you have to download & install XAMPP or any local server that runs PHP scripts. Here’s the link for the XAMPP server https://www.apachefriends.org/index.html.

And, this is the link for the bootstrap that I used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple form for our application. To create a copy of the way and write it into your text editor, then save it as index.php.

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
        <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
    </head>
<body>
    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
        </div>
    </nav>
    <div class="col-md-3"></div>
    <div class="col-md-6 well">
        <h3 class="text-primary">PHP - Odd Numbers Generator</h3>
        <hr style="border-top:1px dotted #ccc;"/>
        <div class="col-md-4">
            <form method="POST">
                <div class="form-inline">
                    <label>Enter a number</label>
                    <input type="number" class="form-control" max="100" name="number" required="required" min="1"/>
                    <button name="generate" class="btn btn-primary">Generate</button>
                </div>
            </form>
            <br />
            <?php
                if(ISSET($_POST['generate'])){
                    $array = [];
                    $number = $_POST['number'];
                    for($i=1; $i<=$number; $i++){
                        array_push($array, $i);
                    }	
 
                    echo implode(", ", $array);
                }
            ?>
        </div>
        <br />
        <div class="col-md-8">	
            <?php include 'generate.php'?>
        </div>
    </div>
</body>
</html>

Creating the Main Function

This code contains the main function of the application. This code will generate an odd number when the button is clicked. To do that just copy and write this block of codes inside the text editor, then save it as generate.php.

<?php
    if(ISSET($_POST['generate'])){
        $array = [];
        $number = $_POST['number'];
        for($i=1; $i<=$number; $i++){
            array_push($array, $i);
            if ($i % 2 != 0){
                $odd[]=$i;
            }
        }
?>
    <center><h4 class="text-warning">Odd Numbers</h4></center>
<?php
    echo implode(", ", $odd);
?>
</div>
 
<?php
    }
?>

There you have it we successfully created Odd Numbers Generator using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Leave a Reply

Your email address will not be published. Required fields are marked *