str_pad

FunctionParams and return types changed in PHP 8.0

Pad a string to a certain length with another string.

PHP 5
PHP 7
PHP 8.0
Improved
PHP 8.1
PHP 8.2-8.3
PHP 8.4
PHP 8.5
PHP 8.6

str_pad Function synopsis

str_pad(
    string $string,
    int $length,
    string $pad_string = " ",
    int $pad_type = STR_PAD_RIGHT
  ): string

Parameters

$string

Typestring

The input string.

$length

Typeint

The desired length of the final padded string. If the value of $length is negative, less than, or equal to the length of the input string, no padding takes place, and $string will be returned.

$pad_string

OptionalTypestringDefault value" "

The $pad_string may be truncated if the required number of padding characters can't be evenly divided by the $pad_string's length.

$pad_type

OptionalTypeintDefault valueSTR_PAD_RIGHT

Optional argument $pad_type can be STR_PAD_RIGHT, STR_PAD_LEFT, or STR_PAD_BOTH. If $pad_type is not specified it is assumed to be STR_PAD_RIGHT.

Return value

Typestring

Returns the padded string.

Changes to the str_pad Function

PHP 8.0

  • Return type added: string
  • Parameter name of parameter #1 changed: $input to $string
  • Parameter type added for parameter #1 ($string): string
  • Parameter name of parameter #2 changed: $pad_length to $length
  • Parameter type added for parameter #2 ($length): int
  • Parameter type added for parameter #3 ($pad_string): string
  • Parameter default value added for position #3 ($pad_string): " "
  • Parameter type added for parameter #4 ($pad_type): int
  • Parameter default value added for position #4 ($pad_type): STR_PAD_RIGHT
  str_pad(
-     $input,
+     string $string,
-     $pad_length,
+     int $length,
-     $pad_string,
+     string $pad_string = " ",
-     $pad_type
+     int $pad_type = STR_PAD_RIGHT
-   )
+   ): string

str_pad Function Availability

PHP VersionAvailability
PHP 8.6Upcoming Release Yes
PHP 8.5Supported (Latest) Yes
PHP 8.4Supported Yes
PHP 8.3Security-Fixes Only Yes
PHP 8.2Security-Fixes Only Yes
PHP 8.1Unsupported Yes
PHP 8.0Unsupported Yes
PHP 7.4Unsupported Yes
PHP 7.3Unsupported Yes
PHP 7.2Unsupported Yes
PHP 7.1Unsupported Yes
PHP 7.0Unsupported Yes
PHP 5.6Unsupported Yes
PHP 5.5Unsupported Yes
PHP 5.4Unsupported Yes
PHP 5.3Unsupported Yes