Avatar ·

Formatting numbers with leading zeros in PHP

Hi everyone! I'm new to PHP and ran into this problem. I need to output order numbers in the format "0001", "0012", "0123" and so on. That is, the number should always occupy 4 characters, and missing digits should be filled with zeros on the left. I tried just concatenating strings, like: ```php $orderNumber = 1; echo "000" . $orderNumber; // outputs "0001" ``` But this only works for numbers from 1 to 9. If the number becomes 10, it will output "00010", but I need "0010". If 100 — "000100" instead of "0100". In short, it's completely wrong. I know that PHP has the str_pad() function, but I don't quite understand how to apply it correctly for numbers. I tried this: ```php $num = 7; echo str_pad($num, 4, "0"); ``` It seems to work, but I'm not sure if this is the best way. Maybe there are some built-in functions