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? Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted July 7, 2008 Share Posted July 7, 2008 Display the value as: sprintf( "%02s", $value ); Quote Link to comment 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. >_> Quote Link to comment 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 ++; ?> Quote Link to comment Share on other sites More sharing options...
papaface Posted July 7, 2008 Share Posted July 7, 2008 Ignore me lol Quote Link to comment 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" } ?> Quote Link to comment 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> Quote Link to comment 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. Quote Link to comment 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) Quote Link to comment 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 Quote Link to comment 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 () Quote Link to comment 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> Quote Link to comment Share on other sites More sharing options...
rollOrDie Posted July 7, 2008 Author Share Posted July 7, 2008 Thanks for the replies! Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.