In Trimming PHP Strings you learned how to remove unwanted whitespace characters from around a string. The opposite of trimming is padding — adding extra characters to either end (or both ends) of a string to make the string a certain length.
Padding is handy for lining up strings vertically for display, for adding leading zeroes to numbers, or for any situation where you need a string to be of a certain fixed length.
In this article you’ll explore PHP’s str_pad()
function for padding strings.