rollOrDie Posted July 7, 2008 Share Posted July 7, 2008 If I set a variable equal to the value 02 is there a way to keep the preceding 0 if I am to increment / decrement it? Link to comment https://forums.phpfreaks.com/topic/113648-keeping-a-0-in-front-of-an-integer/ Share on other sites More sharing options...
roopurt18 Posted July 7, 2008 Share Posted July 7, 2008 Display the value as: sprintf( "%02s", $value ); Link to comment https://forums.phpfreaks.com/topic/113648-keeping-a-0-in-front-of-an-integer/#findComment-584021 Share on other sites More sharing options...
DarkWater Posted July 7, 2008 Share Posted July 7, 2008 @roopurt: Well, I can see him getting potentially confused, so I'll clarify. You're actually going to want to use printf() to display the value, because sprintf() returns the value for assignment to a variable. So yeah. >_> Link to comment https://forums.phpfreaks.com/topic/113648-keeping-a-0-in-front-of-an-integer/#findComment-584023 Share on other sites More sharing options...
rollOrDie Posted July 7, 2008 Author Share Posted July 7, 2008 Thanks, although Im still a little confused! How would I use it to create a script equivalent to the following but keeping the preceding 0? <?php $num = 02; echo $num ++; ?> Link to comment https://forums.phpfreaks.com/topic/113648-keeping-a-0-in-front-of-an-integer/#findComment-584028 Share on other sites More sharing options...
papaface Posted July 7, 2008 Share Posted July 7, 2008 Ignore me lol Link to comment https://forums.phpfreaks.com/topic/113648-keeping-a-0-in-front-of-an-integer/#findComment-584032 Share on other sites More sharing options...
roopurt18 Posted July 7, 2008 Share Posted July 7, 2008 <?php $width = 2; // width of printed value for( $i = 0; $i <= 10; $i++ ) { echo sprintf( "%0{$width}s", $i ) . '<br />'; // or "\n" } ?> Link to comment https://forums.phpfreaks.com/topic/113648-keeping-a-0-in-front-of-an-integer/#findComment-584033 Share on other sites More sharing options...
rollOrDie Posted July 7, 2008 Author Share Posted July 7, 2008 Hmmm Ive got this $width = 2; for ($month = 1; $month <= 12; $month ++) { echo ' <option value="'; echo sprintf ('%0{$width}s', $month); echo '"'; if ($month == $dateMonth) { echo ' selected="selected"'; } echo '>0'; echo sprintf ('%0{$width}s', $month); echo '</option>'; } But its just being output as: <option value="$width}s">0$width}s</option> <option value="$width}s">0$width}s</option> <option value="$width}s">0$width}s</option> <option value="$width}s">0$width}s</option> <option value="$width}s">0$width}s</option> <option value="$width}s" selected="selected">0$width}s</option> <option value="$width}s">0$width}s</option> <option value="$width}s">0$width}s</option> <option value="$width}s">0$width}s</option> <option value="$width}s">0$width}s</option> <option value="$width}s">0$width}s</option> <option value="$width}s">0$width}s</option></select> Link to comment https://forums.phpfreaks.com/topic/113648-keeping-a-0-in-front-of-an-integer/#findComment-584039 Share on other sites More sharing options...
cooldude832 Posted July 7, 2008 Share Posted July 7, 2008 what you are trying to do doesn't make sense to a person who "programs" because an integer doesn't exist with a leading zero as you suggest. Just try and keep what u are doing clear. Link to comment https://forums.phpfreaks.com/topic/113648-keeping-a-0-in-front-of-an-integer/#findComment-584040 Share on other sites More sharing options...
Barand Posted July 7, 2008 Share Posted July 7, 2008 To "people who program" it makes perfect sense. It's octal notation. http://en.wikipedia.org/wiki/Octal echo 012; // ---> 10 (decimal) Link to comment https://forums.phpfreaks.com/topic/113648-keeping-a-0-in-front-of-an-integer/#findComment-584048 Share on other sites More sharing options...
kenrbnsn Posted July 7, 2008 Share Posted July 7, 2008 Since you're dealing with months, I find it's easier to use the date() and strtotime() functions: <?php $tmp = array(); $tmp[] = '<select name="month">'; for ($i=1;$i<13;$i++) { $x = strtotime($i . '/1/'. date('Y')); $tmp[] = '<option value="' . date('m',$x) . '">' . date('F',$x) . '</option>'; } $tmp[] = '</select>'; echo implode("\n",$tmp); ?> Ken Link to comment https://forums.phpfreaks.com/topic/113648-keeping-a-0-in-front-of-an-integer/#findComment-584051 Share on other sites More sharing options...
cooldude832 Posted July 7, 2008 Share Posted July 7, 2008 if u let me type it out to people who program in base 10 system () Link to comment https://forums.phpfreaks.com/topic/113648-keeping-a-0-in-front-of-an-integer/#findComment-584059 Share on other sites More sharing options...
Barand Posted July 7, 2008 Share Posted July 7, 2008 @rollOrDie, simpler is for ($m=1; $m<=12; $m++) { printf("<option value='%02d'>%02d</option>\n", $m, $m); } --> <option value='01'>01</option> <option value='02'>02</option> <option value='03'>03</option> <option value='04'>04</option> <option value='05'>05</option> <option value='06'>06</option> <option value='07'>07</option> <option value='08'>08</option> <option value='09'>09</option> <option value='10'>10</option> <option value='11'>11</option> <option value='12'>12</option> Link to comment https://forums.phpfreaks.com/topic/113648-keeping-a-0-in-front-of-an-integer/#findComment-584063 Share on other sites More sharing options...
rollOrDie Posted July 7, 2008 Author Share Posted July 7, 2008 Thanks for the replies! Link to comment https://forums.phpfreaks.com/topic/113648-keeping-a-0-in-front-of-an-integer/#findComment-584065 Share on other sites More sharing options...
Barand Posted July 7, 2008 Share Posted July 7, 2008 if u let me type it out to people who program in base 10 system () You are probably unique in that respect. There aren't many of you about who program purely in decimal numbers without ever resorting to other bases. For example, hexadecimal values for HTML/CSS colors, octal for chmod values. Link to comment https://forums.phpfreaks.com/topic/113648-keeping-a-0-in-front-of-an-integer/#findComment-584074 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.