jarv Posted April 5, 2011 Share Posted April 5, 2011 hi, I would like to add a leading 0 to my FOR loop for ($i=0; $i<23; $i++) if($i<10){ $i = 0$i; } { echo "<option value=".$i.">" . $i . "</option>"; } this gives and error: unexpected T_VARIABLE at the moment I have: 0 1 2 3 4 5 6 7 8 9 I would like: 00 01 02 03 04 05 06 07 08 09 Quote Link to comment https://forums.phpfreaks.com/topic/232742-if-i-10-then-add-a-leading-0/ Share on other sites More sharing options...
PFMaBiSmAd Posted April 5, 2011 Share Posted April 5, 2011 You would use sprintf with a %02d format specifier - http://us.php.net/sprintf Quote Link to comment https://forums.phpfreaks.com/topic/232742-if-i-10-then-add-a-leading-0/#findComment-1197129 Share on other sites More sharing options...
jarv Posted April 5, 2011 Author Share Posted April 5, 2011 I tried this: for ($i=0; $i<60; $i++) if($i<10){ $i = sprintf("%02d").$i; } { echo "<option value=".$i.">" . $i . "</option>"; } this outputs: 60 Quote Link to comment https://forums.phpfreaks.com/topic/232742-if-i-10-then-add-a-leading-0/#findComment-1197135 Share on other sites More sharing options...
Adam Posted April 5, 2011 Share Posted April 5, 2011 You need to pass $i as the second parameter to sprintf(). Quote Link to comment https://forums.phpfreaks.com/topic/232742-if-i-10-then-add-a-leading-0/#findComment-1197137 Share on other sites More sharing options...
jarv Posted April 5, 2011 Author Share Posted April 5, 2011 thanks, I changed it to this for ($i=0; $i<60; $i++) if($i>10){ $i = sprintf("%02d",$i); } { echo "<option value=".$i.">" . $i . "</option>"; } still only showing 60 Quote Link to comment https://forums.phpfreaks.com/topic/232742-if-i-10-then-add-a-leading-0/#findComment-1197142 Share on other sites More sharing options...
Adam Posted April 5, 2011 Share Posted April 5, 2011 That's because you don't have curly braces around the statements within your FOR loop, so what you're effectively doing is: for ($i=0; $i<60; $i++) { if($i>10) { $i = sprintf("%02d",$i); } } echo "<option value=".$i.">" . $i . "</option>"; In which case only the last value of $i (60) will be shown. In PHP you can add curly braces around any block of code even without a control statement, so this is perfectly valid: { echo "<option value=".$i.">" . $i . "</option>"; } And is why your code appeared to be somewhat structured correctly, but in-fact isn't. Try: for ($i=0; $i<60; $i++) { if($i>10) { $i = sprintf("%02d",$i); } echo "<option value=".$i.">" . $i . "</option>"; } Quote Link to comment https://forums.phpfreaks.com/topic/232742-if-i-10-then-add-a-leading-0/#findComment-1197143 Share on other sites More sharing options...
KevinM1 Posted April 5, 2011 Share Posted April 5, 2011 Also, your conditional is messed up. Aren't you trying to format the numbers less than 10? Quote Link to comment https://forums.phpfreaks.com/topic/232742-if-i-10-then-add-a-leading-0/#findComment-1197145 Share on other sites More sharing options...
Adam Posted April 5, 2011 Share Posted April 5, 2011 I didn't really get the purpose of the IF condition to be honest. Quote Link to comment https://forums.phpfreaks.com/topic/232742-if-i-10-then-add-a-leading-0/#findComment-1197146 Share on other sites More sharing options...
PFMaBiSmAd Posted April 5, 2011 Share Posted April 5, 2011 for ($i=0; $i<60; $i++) { $i = sprintf("%02d",$i); echo "<option value=".$i.">" . $i . "</option>\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/232742-if-i-10-then-add-a-leading-0/#findComment-1197147 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.