PHP - Get the Even & Odd Number in an Array

In this tutorial we will create a Get the Even & Odd Number in an Array using PHP. This code can be use for storing of list of data. PHP is a server-side scripting language designed primarily for web development. Using PHP, you can let your user directly interact with the script and easily to learned its syntax. It is mostly used by a newly coders for its user friendly environment. So Let's do the coding...

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 - Get the Even & Odd Number in an Array</h3>
  16.                 <hr style="border-top:1px dotted #ccc;"/>
  17.                 <form method="POST">
  18.                         <div class="form-inline">
  19.                                 <label>Enter a number</label>
  20.                                 <input type="number" class="form-control" max="100" name="number" required="required" min="1"/>
  21.                                 <button name="generate">Generate Number</button>
  22.                         </div>
  23.                 </form>
  24.                 <br />
  25.                 <h4 class="text-info">Numbers</h4>
  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.                 <br />
  38.                 <hr style="border-top:1px solid #ccc;"/>
  39.                 <?php include 'generate_number.php'?>
  40.         </div>
  41. </body>
  42. </html>

Creating the Main Function

This code contains the main function of the application. This code will separate the even and odd numbers inside the array. To make this just simple copy and write the this block of codes inside the text editor, then save it as generate_number.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.                         }else{
  10.                                 $odd[]=$i;
  11.                         }
  12.                 }
  13. ?>
  14.  
  15. <div class="col-md-6">
  16.         <h4 class="text-warning">EVEN</h4>
  17. <?php
  18.         echo implode(", ", $even);
  19. ?>
  20. </div>
  21. <div class="col-md-6">
  22.         <h4 class="text-warning">ODD</h4>
  23.         <?php
  24.                 echo implode(", ", $odd);
  25.         ?>
  26. </div>
  27.  
  28. <?php
  29.         }
  30. ?>
There you have it we successfully created Create Get the Even & Odd Number in an Array 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