Posted By

sorehead on 10/09/06


Tagged

php numbers Pad in


Versions (?)

Pad Numbers in PHP


 / Published in: PHP
 

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

ADD 0 to 1!

  1. <?php
  2. /* Pad Numbers in PHP
  3.  
  4. 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. */
  5.  
  6.  
  7. $raw = 1;
  8. $formatted = str_pad($raw, 2, 0, STR_PAD_LEFT);
  9. echo $formatted;
  10.  
  11. //outputs 01
  12. ?>

Report this snippet  

You need to login to post a comment.