Backend Development 2 min read

PHP str_pad() Function: Usage, Parameters, Return Value, and Examples

The article explains PHP's str_pad() function, detailing how it pads a string to a specified length with optional padding characters and types, describes each parameter and return value, and provides multiple code examples demonstrating right, left, and both‑side padding.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
PHP str_pad() Function: Usage, Parameters, Return Value, and Examples

str_pad() pads a string to a specified length using another string.

Parameters

input : the input string.

pad_length : desired length; if negative or less than or equal to the input length, no padding occurs.

pad_string (optional): string used for padding; if its length does not divide evenly, it may be truncated.

pad_type (optional): STR_PAD_RIGHT, STR_PAD_LEFT or STR_PAD_BOTH; default is STR_PAD_RIGHT.

Return value

The padded string, padded on the left, right, or both sides according to the specified parameters; if pad_string is omitted, spaces are used.

Examples

BackendphpString()functionPaddingstr_pad
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.