Over a million developers have joined DZone.

Pad Numbers In PHP

DZone's Guide to

Pad Numbers In PHP

Free Resource
Sometimes I need numbers to always be two digits, even if they're less than 10. The below snippet will format all numbers to be two digits, inserting a 0 if they're under 10 - creating, for example, 09 instead of 9.

$raw = 1;
$formatted = sprintf("%02d", $raw);
echo $formatted;

//outputs 01

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}