Home >Backend Development >PHP Tutorial >How Can I Zero-Pad Single-Digit Numbers in a String Using PHP?
Zero-Pad Digits in String
Problem:
You need to convert single-digit numbers (1 to 9) into strings with leading zeros (e.g., convert 5 to 05). You're seeking a concise and efficient solution for this task.
Solution:
The sprintf function provides a straightforward way to achieve this:
$s = sprintf('%02d', $digit);
Additional Notes:
The above is the detailed content of How Can I Zero-Pad Single-Digit Numbers in a String Using PHP?. For more information, please follow other related articles on the PHP Chinese website!