Jump to content

[SOLVED] Leading zero's in for loop


realjumper

Recommended Posts

Hi,

 

in this little loop I am getting the count of 0 - 31. It is giving '1,2,3,4,5,6,7,8,9,10 etc etc

 

I would like the leading zero to be there for the single digit numbers, like this '01,02,03,04,05,06,07,08,09,10,11 etc etc.

 

What am I not doing?

 


for ($num=1; $num <= 31; $num++ ) 
   { 
    echo "<option value=\"$num\">$num</option>"; 
   } 

 

Thanks :-)

Link to comment
https://forums.phpfreaks.com/topic/39698-solved-leading-zeros-in-for-loop/
Share on other sites

you could write a simple function to pad the zero such as

<?php

function padzero($num)
{
if ($num < 10)
{
$num = "0$num";
}
return $num;
}

//then 

for ($num=1; $num <= 31; $num++ ) 
   { 
    echo "<option value=\"$num\">padzero($num)</option>"; //value stays the same what is shown has padded zero
   } 

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.