Moron Posted November 25, 2008 Share Posted November 25, 2008 This is probably dirt simple, but I'm having a problem finding it in the manual. In the database, if a number is five digits, my code echoes the entire five digits (12345), of course. If it's a single digit, I want to append four zeros to the beginning of it (00004, etc...). How is this done? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/134264-solved-append-zeros-to-a-number/ Share on other sites More sharing options...
DeanWhitehouse Posted November 25, 2008 Share Posted November 25, 2008 I think you will need to write your own function for this. Gimme me a minute and i will write one. Quote Link to comment https://forums.phpfreaks.com/topic/134264-solved-append-zeros-to-a-number/#findComment-698949 Share on other sites More sharing options...
DeanWhitehouse Posted November 25, 2008 Share Posted November 25, 2008 <?php function add_zero($num) { if(strlen($num) != 5) { $i = 0; $diff = 5 - strlen($num); while($i < $diff) { $num = 0 .$num; $i++; } return $num; } else { return $num; } } echo add_zero(1); echo "<br>"; echo add_zero(204); echo "<br>"; echo add_zero(12345); ?> Quote Link to comment https://forums.phpfreaks.com/topic/134264-solved-append-zeros-to-a-number/#findComment-698956 Share on other sites More sharing options...
PFMaBiSmAd Posted November 25, 2008 Share Posted November 25, 2008 http://us3.php.net/str_pad Quote Link to comment https://forums.phpfreaks.com/topic/134264-solved-append-zeros-to-a-number/#findComment-698960 Share on other sites More sharing options...
DeanWhitehouse Posted November 25, 2008 Share Posted November 25, 2008 Damn, lol Quote Link to comment https://forums.phpfreaks.com/topic/134264-solved-append-zeros-to-a-number/#findComment-698962 Share on other sites More sharing options...
revraz Posted November 25, 2008 Share Posted November 25, 2008 Can't you use print_r for this? Quote Link to comment https://forums.phpfreaks.com/topic/134264-solved-append-zeros-to-a-number/#findComment-698967 Share on other sites More sharing options...
trq Posted November 25, 2008 Share Posted November 25, 2008 Can't you use print_r for this? You meen printf? printf('%05d',23); Quote Link to comment https://forums.phpfreaks.com/topic/134264-solved-append-zeros-to-a-number/#findComment-698973 Share on other sites More sharing options...
revraz Posted November 25, 2008 Share Posted November 25, 2008 Yep, printf... Quote Link to comment https://forums.phpfreaks.com/topic/134264-solved-append-zeros-to-a-number/#findComment-699018 Share on other sites More sharing options...
DeanWhitehouse Posted November 25, 2008 Share Posted November 25, 2008 Or you could use my solution Quote Link to comment https://forums.phpfreaks.com/topic/134264-solved-append-zeros-to-a-number/#findComment-699026 Share on other sites More sharing options...
flyhoney Posted November 25, 2008 Share Posted November 25, 2008 <?php $number = 5; echo str_pad($number, 5, '0', STR_PAD_LEFT); ?> 00005 Quote Link to comment https://forums.phpfreaks.com/topic/134264-solved-append-zeros-to-a-number/#findComment-699028 Share on other sites More sharing options...
Moron Posted November 26, 2008 Author Share Posted November 26, 2008 printf() did the trick. Thanks, everyone! Quote Link to comment https://forums.phpfreaks.com/topic/134264-solved-append-zeros-to-a-number/#findComment-699660 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.