Skip to content

Instantly share code, notes, and snippets.

@joshuabaker
Created April 4, 2011 14:38
Show Gist options
  • Save joshuabaker/901745 to your computer and use it in GitHub Desktop.
Save joshuabaker/901745 to your computer and use it in GitHub Desktop.
Pad a string or zero fill a number using PHP.
<?php
$number = 5;
echo sprintf('%02s', $number); // 05
echo sprintf('%03s', $number); // 005
echo sprintf('%02s', 453); // 453
// sprintf is faster than str_pad
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment