str_pad() Function in PHP 8.2, PHP 8.3 & PHP 8.4

The str_pad() function in PHP is used to pad a string to a certain length with another string. It works the same in PHP 8, PHP 8.1, PHP 8.2, PHP 8.3 and PHP 8.4, as there have been no changes to this function in these versions.

Examples

1. Pad Right (default)

<?php
echo str_pad("PHP", 10, "."); // Output: "PHP......."
?>

2. Pad Left

<?php
echo str_pad("PHP", 10, ".", STR_PAD_LEFT); // Output: ".......PHP"
?>

3. Pad Both

<?php
echo str_pad("PHP", 10, ".", STR_PAD_BOTH); // Output: "...PHP...."
?>

4. Pad with multi-character string

<?php
echo str_pad("42", 10, "0"); // Output: "4200000000"
?>
  1. If $length is less than or equal to the length of $string, the input string is returned unchanged.
  2. $pad_string can be more than one character long; PHP repeats it as needed and may cut it to fit.