Return to Snippet

Revision: 1452
at October 9, 2006 11:19 by sorehead


Updated Code
<?php
/* Pad Numbers in PHP

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 = str_pad($raw, 2, 0, STR_PAD_LEFT);
echo $formatted;

//outputs 01
?>

Revision: 1451
at October 9, 2006 11:17 by sorehead


Updated Code
<?php
/* Pad Numbers in PHP

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
?>

Revision: 1450
at October 9, 2006 11:17 by sorehead


Initial Code
<?php
/* Pad Numbers in PHP

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
?>

Initial URL
http://www.bigbold.com/snippets/posts/show/2217

Initial Description
ADD 0 to 1!

Initial Title
Pad Numbers in PHP

Initial Tags
php

Initial Language
PHP