How to Add / Remove leading zeros before number in PHP ?

What is a simple way how to add / remove leading zeros before number in PHP ?
0
give a positive ratinggive a negative rating
17 Oct 2021 at 03:00 PM
Hi,

There are multiple methods available how to add or remove leading zeros before number.

To add leading zero before number, you can use sprintf(). The following function will add leading zero before a single digit number:

$value = sprintf('%02d', $number);
To remove leading zeros before number, you can use ltrim() or (int):

$value = ltrim($number, "0");
$value = (int)$number;

0
give a positive ratinggive a negative rating
18 Oct 2021 at 11:57 AM
Tim
Share on FacebookShare on TwitterShare on LinkedInSend email
x
x
Follow us on Facebook
2022 AnswerTabsTermsContact us