Jump to content

Adding digits to beginning of number.


Veest

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/125317-adding-digits-to-beginning-of-number/
Share on other sites

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.