numbers in php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anfetienne
    Contributor
    • Feb 2009
    • 424

    numbers in php

    Hi,

    i've looked through the hp manual and cant find a function for this... is it possible to echo out double figures instead of single in php? for exmaple

    Code:
        for($i=00;$i<23;$i++){
        	echo '<option value="'.$i .'">'.$i .'</option>';
        }
    this lists as 1,2,3,4,5,6,7,8 ,9,10 and i want it to be 01,02,03,04,05, 06,07,08,09

    is this possible?
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hey.

    Check out the printf function. You can have it format the values you pass it in a number of ways, including the way you are asking.

    For example:
    [code=php]<?php
    printf("%03d\n" , 7);
    // This prints: 007
    ?>[/code]

    Comment

    • anfetienne
      Contributor
      • Feb 2009
      • 424

      #3
      Thanks Atli... that's perfect!

      Comment

      • anfetienne
        Contributor
        • Feb 2009
        • 424

        #4
        i tested it outside my loop and it outputs correctly but once inside my loop it still outputs single digits..... strange

        Comment

        • anfetienne
          Contributor
          • Feb 2009
          • 424

          #5
          i fixed it... here is my solution.... THANKS AGAIN

          Code:
          	for($i=0;$i<23;$i++){
              	echo '<option value="'; printf("%02d\n", $i); echo '">'; printf("%02d\n", $i); echo '</option>';
              }

          Comment

          Working...