Veest Posted September 22, 2008 Share Posted September 22, 2008 Heya, My friend gave me this piece of PHP coding which should add extra 0's onto the beginning of a number depending on how large the number is. For example 0000001, 0000030, 0000658 etc etc. However at the moment, all its doing is giving me the number without the extra 0's in! while($l=mysql_fetch_array($result)){ $i=0; while($i<1){ $id=$l[$i]; $ii=0; $number=''; while($ii<(7-$id)){ $number.='0'; $ii++; } $number.=$id; $out .='"Barcode","'.$id.'","LSO"'; $i++; } } Does anyone have any ideas what is wrong with the coding? I'm sure its easy to fix for all you PHP Experts! Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/125317-adding-digits-to-beginning-of-number/ Share on other sites More sharing options...
PFMaBiSmAd Posted September 22, 2008 Share Posted September 22, 2008 That code may or may not do what you want, but as soon as you reference the results as a number any leading zeros will be lost. You must always reference the result as a string in order to keep any leading zeros. All that code can be replaced with the php str_pad() function. Quote Link to comment https://forums.phpfreaks.com/topic/125317-adding-digits-to-beginning-of-number/#findComment-647783 Share on other sites More sharing options...
Veest Posted September 22, 2008 Author Share Posted September 22, 2008 Fantastic! This has worked! Thank you very much! Quote Link to comment https://forums.phpfreaks.com/topic/125317-adding-digits-to-beginning-of-number/#findComment-647786 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.