PHP prepend leading zero prefixes after number

PHP prepend leading zero prefixes after number

In this Post We Will Explain About is PHP prepend leading zero prefixes after number With Example and Demo.Welcome on Pakainfo.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to Formatting a number with leading zeros in PHPExample

In this post we will show you Best way to implement How to format numbers with 00 prefixes in php, hear for PHP prepend leading zero before single digit numberwith Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

php add leading zero to single digit number

here PHP Source code to Following The Many PHP function to used php generate padding prefixes after and before append new prefixes value using PHP Like as a (str_pad() function, sprintf() function or custom function )

PHP Input id

Input : 8 ->   Output : Invoice 0008
Input : 10 ->  Output : Invoice 0010
Input : 180 -> Output : Invoice 0180

Example 1 : Use str_pad() – (php add leading zero to single digit number)

$invoiceId = 51;
$invID = str_pad($invoiceId, 4, '0', STR_PAD_LEFT);

Example 2 : convert prefixes (0000) – (PHP prepend leading zero before single digit number)

$invoiceId = 51;
$invoiceId = sprintf('%04d',$invoiceId);
print $invoiceId;
// outputs 0051

$invoiceId = 8051;
$invoiceId = sprintf('%04d',$invoiceId);
print $invoiceId;
// outputs 8051

Example 3 : printf() function – (How to format numbers with 00 prefixes in php)

printf('%04d',$invoiceId);

Example 4 : sprintf() – (Format a number with grouped thousands)

$xid = 1;
sprintf("%03d",$xid);
echo $xid;

Example 5 : sprintf function Based.(Formatting a number with leading zeros in PHP)

function convert_number_prefix($value, $threshold = 2) {
    return sprintf('%0' . $threshold . 's', $value);
}

convert_number_prefix(1);      // 01
convert_number_prefix(5);      // 05
convert_number_prefix(100);    // 100
convert_number_prefix(1);      // 001
convert_number_prefix(5, 3);   // 005
convert_number_prefix(100, 3); // 100
convert_number_prefix(1, 7);   // 0000001

Example

I hope you have Got What is Formatting a number with leading zeros in PHP And how it works.I would Like to have FeadBack From My Blog(Pakainfo.com) readers.Your Valuable FeadBack,Any Question,or any Comments abaout This Article(Pakainfo.com) Are Most Always Welcome.

Leave a Comment