Pad Numbers in PHP


/ Published in: PHP
Save to your folder(s)

ADD 0 to 1!


Copy this code and paste it in your HTML
  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. ?>

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.