PHP str_pad() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, str_pad() pads a string to a given length by adding characters to the left, right, or both sides.

It is great for aligning text, adding leading zeros to numbers, or building simple text tables.

Syntax

Call str_pad() with the values you need. Start with this line.

PHP
$padded = str_pad("42", 5, "0", STR_PAD_LEFT);
CallWhat it does
str_pad("42", 5, "0", STR_PAD_LEFT)Returns "00042".
str_pad("Hi", 6, "*")Returns "Hi****" (pads on the right by default).
str_pad("PHP", 9, "-=", STR_PAD_BOTH)Returns "-=-PHP-=-".

The pad type can be STR_PAD_RIGHT (default), STR_PAD_LEFT, or STR_PAD_BOTH. If the target length is not larger than the string, the string is returned unchanged.

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

Parameters

ParameterDescription
stringRequired. The input string to pad.
lengthRequired. The final length you want. If it is smaller than the string, nothing is added.
pad_stringOptional. The text used for padding. Default is a space. It cannot be empty.
pad_typeOptional. STR_PAD_RIGHT (default), STR_PAD_LEFT, or STR_PAD_BOTH.

Return Value

Return ValueDescription
stringThe padded string.

PHP Examples

Example 1 — Add Leading Zeros

A classic use: format a number to a fixed width.

PHP
<?php
$id = "42";
$padded = str_pad($id, 5, "0", STR_PAD_LEFT);

echo $padded, PHP_EOL;

How It Works

  1. Store the number as a string.
  2. Ask for a total length of 5 characters.
  3. Use "0" as the pad text and STR_PAD_LEFT to add on the left.
  4. PHP adds three zeros to make 00042.

Example 2 — Length Smaller Than the String

If the target length is too small, nothing changes.

PHP
<?php
$text = "Hello";

echo str_pad($text, 3, "*"), PHP_EOL;

How It Works

  1. The string "Hello" already has 5 characters.
  2. The target length is only 3.
  3. There is nothing to pad.
  4. str_pad returns the original string and never cuts it.

Example 3 — Center Text with STR_PAD_BOTH

Pad on both sides with a repeating pattern.

PHP
<?php
echo str_pad("PHP", 9, "-=", STR_PAD_BOTH), PHP_EOL;

How It Works

  1. "PHP" has 3 characters and the target is 9, so 6 are added.
  2. STR_PAD_BOTH splits them: 3 on the left and 3 on the right.
  3. The pad text "-=" repeats and is cut to fit: "-=-".
  4. The result looks centered: -=-PHP-=-.

Frequently Asked Questions

str_pad() makes a string longer by adding a pad string to its left, right, or both sides until it reaches the length you choose.
Use str_pad($number, 5, '0', STR_PAD_LEFT). It turns '42' into '00042'. For numbers you can also use sprintf('%05d', $number).
PHP returns the original string unchanged. str_pad() never shortens a string.
STR_PAD_RIGHT adds to the end (default), STR_PAD_LEFT adds to the start, and STR_PAD_BOTH adds to both sides.
No. An empty pad_string throws a ValueError in PHP 8. Use a space or another visible character.

Next: str_repeat()

str_repeat() repeats a string a given number of times and returns the combined result.

str_repeat() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful