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.

$pad_types = STR_PAD_LEFT, STR_PAD_RIGHT, STR_PAD_BOTH

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

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.

Arjun
I am Arjun from Hyderabad (India). I have been working as a software engineer from last 7+ years, and its my passion to learn new things and implement them as a practice. Aside from work, I likes gardening and spending time with pets.
DMCA.com Protection Status