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 ?
17 Oct 2021 at 03:00 PM
0give a positive ratinggive a negative ratingshow more
LarsLars
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;

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