PHP - Odd Numbers Generator

In this tutorial we will create a Odd Numbers Generator using PHP. This code will instantly separate all the possible odd numbers in the web page. This code use a PHP POST method to generate an odd number dynamically in an array of number using an algebraic conditional statement .This is a user-friendly kind of program feel free to modify it. We will be using PHP as a scripting language that manage a database server to handle a bulk of data per transaction. It describe as an advance technology that manage both server and control-block of your machine.

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for 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 the forms simply copy and write it into your text editor, then save it as index.php.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">PHP - Odd Numbers Generator</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-4">
  18. <form method="POST">
  19. <div class="form-inline">
  20. <label>Enter a number</label>
  21. <input type="number" class="form-control" max="100" name="number" required="required" min="1"/>
  22. <button name="generate" class="btn btn-primary">Generate</button>
  23. </div>
  24. </form>
  25. <br />
  26. <?php
  27. if(ISSET($_POST['generate'])){
  28. $array = [];
  29. $number = $_POST['number'];
  30. for($i=1; $i<=$number; $i++){
  31. array_push($array, $i);
  32. }
  33.  
  34. echo implode(", ", $array);
  35. }
  36. ?>
  37. </div>
  38. <br />
  39. <div class="col-md-8">
  40. <?php include 'generate.php'?>
  41. </div>
  42. </div>
  43. </body>
  44. </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.
  1. <?php
  2. if(ISSET($_POST['generate'])){
  3. $array = [];
  4. $number = $_POST['number'];
  5. for($i=1; $i<=$number; $i++){
  6. array_push($array, $i);
  7. if ($i % 2 != 0){
  8. $odd[]=$i;
  9. }
  10. }
  11. ?>
  12. <center><h4 class="text-warning">Odd Numbers</h4></center>
  13. <?php
  14. echo implode(", ", $odd);
  15. ?>
  16. </div>
  17.  
  18. <?php
  19. }
  20. ?>
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!

Add new comment