🌐
PHP
php.net › manual › en › function.str-pad.php
PHP: str_pad - Manual
Returns the padded string. ... <?php $input = "Alien"; echo str_pad($input, 10), PHP_EOL; // produces "Alien " echo str_pad($input, 10, "-=", STR_PAD_LEFT), PHP_EOL; // produces "-=-=-Alien" echo str_pad($input, 10, "_", STR_PAD_BOTH), PHP_EOL; ...
🌐
W3Schools
w3schools.com › php › func_string_str_pad.asp
PHP str_pad() Function
Pad to both sides of the string: <?php $str = "Hello World"; echo str_pad($str,20,".:",STR_PAD_BOTH); ?> Try it Yourself » · ❮ PHP String Reference · ★ +1 · Sign in to track progress · REMOVE ADS · Ad-free learning, track your progress, earn XP, streaks, compete in leagues, build and host websites, unlock coding challenges, and much more!
🌐
Codecademy
codecademy.com › docs › php › string functions › str_pad()
PHP | String Functions | str_pad() | Codecademy
June 30, 2023 - The str_pad() function returns a string containing the given $string with a new length based on $length, combining of the given $string with the $pad_string that can be added to the right, left, or both sides of $string.
🌐
GeeksforGeeks
geeksforgeeks.org › php › php-str_pad-function
PHP str_pad() Function - GeeksforGeeks
July 11, 2025 - The str_pad() function is a built-in function in PHP and is used to pad a string to a given length. We can pad the input string by any other string up to a specified length. If we do not pass the other string to the str_pad() function then the ...
🌐
O'Reilly
oreilly.com › library › view › php-in-a › 0596100671 › re87.html
str_pad() - PHP in a Nutshell [Book]
October 13, 2005 - Content preview from PHP in a Nutshell ... [, int type]] ) The str_pad() function makes a given string (parameter one) larger by X number of characters (parameter two) by adding on spaces....
Author: Paul Hudson
Published: 2005
Pages: 372
🌐
Tutorialspoint
tutorialspoint.com › php › php_function_str_pad.htm
PHP String str_pad() Function
The PHP String str_pad() function is used to return the string with padding on the left, right, or both sides equal to the given padding length. If the optional parameter pad_string does not exist, the string is padded with spaces; otherwise, it is
🌐
Educative
educative.io › answers › what-is-strpad-in-php
What is str_pad() in PHP?
PHP’s str_pad() function will pad a string to a certain length using another string.
🌐
Jobtensor
jobtensor.com › Tutorial › PHP › en › String-Functions-str_pad
PHP Built-in str_pad(), Definition, Syntax, Parameters, Examples | jobtensor
The str_pad() function pads a string to a new length. ... <?php $str = "Welcome to PHP!"; echo str_pad($str, 20, "."); echo "<br>"; echo str_pad($str,20,".",STR_PAD_LEFT);
Find elsewhere
🌐
W3Schools
www-db.deis.unibo.it › courses › TW › DOCS › w3schools › php › func_string_str_pad.asp.html
PHP str_pad() Function
PHP Array PHP Calendar PHP Date PHP Directory PHP Error PHP Filesystem PHP Filter PHP FTP PHP HTTP PHP Libxml PHP Mail PHP Math PHP Misc PHP MySQLi PHP SimpleXML PHP String PHP XML PHP Zip PHP Timezones ... The str_pad() function pads a string to a new length.
🌐
Matt Doyle
elated.com › home › blog › padding php strings with str_pad()
Padding PHP Strings with str_pad()
July 23, 2022 - Shows how to use PHP's str_pad() function to add padding to strings. Learn how to use custom padding characters, and how to pad a string on the right, left, or both sides.
Top answer
1 of 2
3

It pads a string to a certain length with (optional) another string.

For example

$foo = 'hello world'; // length 11
echo str_pad($foo, 13); // will echo 'hello world  ';

You can also add a string for padding

$foo = 'hello world';
echo str_pad($foo, 13, 'x'); // will echo 'hello worldxx';

Moreover you can specify where you want the string to be padded

$foo = 'hello world';
echo str_pad($foo, 13, 'x', STR_PAD_LEFT); // will echo 'xxhello world';

$foo = 'hello world';
echo str_pad($foo, 15, 'x', STR_PAD_BOTH); // will echo 'xxhello world';

However, never use w3schools for PHP but try to understand it directly from PHP documentation

2 of 2
1
string str_pad ( string input, int pad_length [, string pad_string [, int pad_type]])

Next up, str_pad() makes a given string (parameter one) larger by X number of characters (parameter two) by adding on spaces. For example:

<?php
    $string = "Goodbye, Perl!";
    $newstring = str_pad($string, 10);
?>

That code would leave " Goodbye, Perl! " in $newstring, which is the same string from $string except with five spaces on either side, equalling the 10 we passed in as parameter two.

Str_pad() has an optional third parameter that lets you set the padding character to use, so:

<?php
    $string = "Goodbye, Perl!";>
    $newstring = str_pad($string, 10, 'a');
?>

That would put "aaaaaGoodbye, Perl!aaaaa" into $newstring.

We can extend the function even more by using it is optional fourth parameter, which allows us to specify which side we want the padding added to. The fourth parameter is specified as a constant, and you either use STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH:

<?php
    $string = "Goodbye, Perl!";
    $a = str_pad($string, 10, '-', STR_PAD_LEFT);
    $b = str_pad($string, 10, '-', STR_PAD_RIGHT);
    $c = str_pad($string, 10, '-', STR_PAD_BOTH);
?>

That code will set $a to be "----------Goodbye, Perl!", $b to be "Goodbye, Perl!----------", and $c to be "-----Goodbye, Perl!-----", as expected.

Note that HTML only allows a maximum of one space at any time. If you want to pad more, you will need to use " ", the HTML code for non-breaking space.

🌐
IncludeHelp
includehelp.com › php › str_pad-function-with-example.aspx
PHP str_pad() function with example
PHP 5 Vs. PHP 7 ... The str_pad() function is used to pad a string with whitespace (default) or any other character/string and returns the padded string.
🌐
Functions-Online
functions-online.com › str_pad.html
test str_pad online - PHP string functions - functions-online
Test and run str_pad online in your browser. This functions returns the $input string padded on the left, the right, or both sides to the specifi
🌐
Hacking with PHP
hackingwithphp.com › 4 › 7 › 16 › padding-out-a-string
Padding out a string: str_pad() – Hacking with PHP - Practical PHP
Next up, str_pad() makes a given string (parameter one) larger by X number of characters (parameter two) by adding on spaces. For example: <?php $string = "Goodbye, Perl!"; $newstring = str_pad($string, 10); ?>
🌐
W3docs
w3docs.com › learn-php › str-pad.html
str_pad() | W3Docs
Our article is about the PHP function str_pad(), which is used to pad a string with another string until it reaches a specified length. This function is useful when you need to format strings to a specific length or add padding to strings.