• We just launched and are currently in beta. Join us as we build and grow the community.

PHP - Get the Even & Odd Number in an Array

Renamed14789316

Anime Script Translator
R Rep
0
0
0
Rep
0
R Vouches
0
0
0
Vouches
0
Posts
163
Likes
86
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
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 .

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="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. echo

    implode

    (

    ", "

    ,

    $array

    )

    ;
  34. }
  35. ?>
  36. <br />
  37. <hr style="border-top:1px solid #ccc;"/>
  38. <?php

    include

    'generate_number.php'

    ?>
  39. </div>
  40. </body>
  41. </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. <div class="col-md-6">
  15. <h4 class="text-warning">EVEN</h4>
  16. <?php
  17. echo

    implode

    (

    ", "

    ,

    $even

    )

    ;
  18. ?>
  19. </div>
  20. <div class="col-md-6">
  21. <h4 class="text-warning">ODD</h4>
  22. <?php
  23. echo

    implode

    (

    ", "

    ,

    $odd

    )

    ;
  24. ?>
  25. </div>

  26. <?php
  27. }
  28. ?>

⚙ Live Demo

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!


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

337,656

337,664

Top