In need of a bit of help with this program

Discussion in 'PHP' started by demondestiny, Aug 13, 2010.

  1. #1
    Hi all i'm developing a console mode program that will prompt the user for a start and end number and then display all numbers in order between the start and end number. So for example if a user types in 10 for the start and 20 for the end it should come out like this,

    10,11,12,13,14,15,16,17,18,19,20

    Heres the code so far i know i've probably made an amatuer mistake any help would be much appreciated.

    
    #!/usr/local/bin/php -q
    <?php
    //phpprogram.php
    require ("phpio");
    print("\n\n");
    print ("enter start number : ");
    $start = input();
    print("\n\n");
    print ("enter end number : ");
    $end = input();
    while ($end <=$end)
    {
      print("\n  ");
      print($start);
      $start = $end +1;
    }
    print("\n\n   ");
    ?>
    
    Code (markup):
    Thanks.
     
    demondestiny, Aug 13, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    echo implode(',', range($start, $end));
    PHP:
     
    danx10, Aug 13, 2010 IP
  3. demondestiny

    demondestiny Peon

    Messages:
    65
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks i got it working but now it's doing a continuous loop. This is what the code looks like now not sure if i placed it incorrectly or not?

    
    #!/usr/local/bin/php -q
    <?php
    //phpprogram.php
    require ("phpio");
    print("\n\n");
    print ("enter start number : ");
    $start = input();
    print("\n\n");
    print ("enter end number : ");
    $end = input();
    while ($start <=$end)
    {
      print("\n");
      echo implode(',', range($start, $end));
    }
    print("\n\n   ");
    ?>
    
    Code (markup):
     
    demondestiny, Aug 13, 2010 IP
  4. Thorlax402

    Thorlax402 Member

    Messages:
    194
    Likes Received:
    2
    Best Answers:
    5
    Trophy Points:
    40
    #4
    You don't need a loop with the range function. It will create the output you want on it's own.
     
    Thorlax402, Aug 13, 2010 IP
  5. demondestiny

    demondestiny Peon

    Messages:
    65
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks to you both it's working properly now.
     
    demondestiny, Aug 13, 2010 IP