Zero Padded Numbers with php?

Today I got a situation where I need automatically zero filled number. I realized it could be done much str_pad. The reason I am writing this post is, I just want to share with my readers.

str_pad — Pad a string to a certain length with another string. String pad is used to make a given string larger by N number of characters by adding specified string for padding , default is white space.


Following are the notes –

  • If pad_type is not specified it is assumed to be STR_PAD_RIGHT.
  • If not an even number, the right side gets the extra padding.
  • If the optional argument pad_string is not supplied, the input is padded with spaces
  • Specifies the pad_string length. If this value is less than the original length of the string, nothing will be happened.

Examples :

Simple Function to get Zero Padded Numbers

By Arjun

I am Arjun from Hyderabad (India). I have been working as a software engineer from the last 7+ years, and it is my passion to learn new things and implement them as a practice. Aside from work, I like gardening and spending time with pets.

Leave a Reply

Your email address will not be published. Required fields are marked *