PHP: Using str_pad not working? Why?

php, string-concatenation

Solution

The second argument to `str_pad()` takes the full length of the final string; because you're passing 4 and the length of `$myString` is 5, nothing will happen.

You should choose a width that's at least one bigger than your example value, e.g.:

str_pad($myString, 9, '0', STR_PAD_LEFT);
// "000024896"

Update

This might be obvious, but if you always want 4 zeros in front of whatever `$myString` is:

'0000' . $myString;

Problem

I have a string, its content is "24896". Now I want to add some zeros to the left, so I tried: ``` $test = str_pad($myString, 4, "0", STR_PAD_LEFT); ``` The result is "24896" again, no zeros added to the left. Am I missing something here? Thanks!

Original source