PHP - Even Numbers Generator

Submitted by: 
Visitors have accessed this post 276 times.

In this tutorial we will create a Even Numbers Generator using PHP. This code will instantly separate all the possible even numbers in the form. This code itself use a simple POST method to generate an even number dynamically within the array of number using a 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

And, this is the link for the bootstrap that i used for the layout design

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="<a href="">Sourcecodester</a>
  11. " rel="nofollow">">Sourcecodester</a>
  12. </a> </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">PHP - Even Numbers Generator</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <div class="col-md-4">
  19. <form method="POST">
  20. <div class="form-inline">
  21. <label>Enter a number</label>
  22. <input type="number" class="form-control" max="100" name="number" required="required" min="1"/>
  23. <button name="generate" class="btn btn-primary">Generate</button>
  24. </div>
  25. </form>
  26. <br />
  27. <?php
  28. if(ISSET($_POST['generate'])){
  29. $array = [];
  30. $number = $_POST['number'];
  31. for($i=1; $i<=$number; $i++){
  32. array_push($array, $i);
  33. }
  35. echo implode(", ", $array);
  36. }
  37. ?>
  38. </div>
  39. <br />
  40. <div class="col-md-8">
  41. <?php include 'generate.php'?>
  42. </div>
  43. </div>
  44. </body>
  45. </html>

Creating the Main Function

This code contains the main function of the application. This code will generate an even 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. $even[]=$i;
  9. }
  10. }
  11. ?>
  12. <center><h4 class="text-warning">EVEN Numbers</h4></center>
  13. <?php
  14. echo implode(", ", $even);
  15. ?>
  16. </div>
  18. <?php
  19. }
  20. ?>

There you have it we successfully created Even 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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.