Categories: PHP

str_pad – PHP String Functions

Syntax :

str_pad (string, length, pad_string, pad_type );

Description :

str_pad() function will pad a string to a certain length with another string.


Parameter :

  • string – This is a Required parameter. It is the input string on which function will be performed.
  • length – This is a Required parameter. It sets value of new length of a input string parameter. if the value of pad_length is negative, less than, or equal to the length of the input string, no padding takes place, and input will be returned.
  • pad_string – This is a Required parameter. It sets string to be padded with input string. Default padded string is whitespace.
  • pad_type – This is an Optional parameter. It can be Specifies what side to pad the string.
    • STR_PAD_RIGHT – Padding is done to the right side of the string. This is default value.
    • STR_PAD_LEFT – Padding is done to the left side of the string
    • STR_PAD_BOTH – Padding is done to both sides of the string. If not an even number, the right side gets the extra padding.
Note : The pad_string may be truncated if the required number of padding characters can’t be evenly divided by the pad_string’s length.

Output :

This will return a padded string.


Related articles : str_repeat().


str_pad() – PHP Functions Example 1 :
<?php
$strExample = "Hi from tutorialmines."; 
echo str_pad($strExample,30,"!");
?>

In above example ,We have a string “Hi from tutorialmines.”. Now this function will pad the string “!” in the right side of string and makes its length to 30. Output of above code in the browser is as below:

Hi from tutorialmines.!!!!!!!!

str_pad() – PHP Functions Example 2 : Padding string “!” on LEFT side of string.
<?php
$strExample = "Hi from tutorialmines."; 
echo str_pad($strExample,30,"!",STR_PAD_LEFT);
?>

In above example ,We have a string “Hi from tutorialmines.”. Now this function will pad the string “!” in the LEFT side of string and makes its length to 30. Output of above code in the browser is as below:

!!!!!!!!Hi from tutorialmines.

str_pad() – PHP Functions Example 3 : Padding string “!” on BOTH side of string.
<?php
$strExample = "Hi from tutorialmines."; 
echo str_pad($strExample,30,"!",STR_PAD_BOTH);
?>

In above example ,We have a string “Hi from tutorialmines.”. Now this function will pad the string “!” in the LEFT and RIGHT both side of string and makes its length to 30. Output of above code in the browser is as below:

!!!!Hi from tutorialmines.!!!!


str_pad() – PHP Functions Example 4 : length value is less than string length.
<?php
$strExample = "Hi from tutorialmines."; 
echo str_pad($strExample,20,"!");
?>

In above example ,We have a string “Hi from tutorialmines.”. Now str_pad() length values is less than the length of input string. So, nothing will be padded and the inpu string will be returned. Output of above code in the browser is as below:

Hi from tutorialmines.

jyoti rani

Recent Posts

Modern Toys, Magical Moments: Why the Best Toy Shop in Noida Is More Than Just a Store

When it comes to children, there’s one universal truth: the right toy can spark imagination, build skills, and make memories…

6 months ago

Rediscovering Joy: A New Era of Creativity & Comfort in Toy Stores

In today’s digital age, where screens and gadgets dominate our children’s lives, there’s something heartwarming about a well-loved plush toy…

6 months ago

Unboxing Imagination: Discovering the Joy of Play at a Toy Store in Noida

In a world dominated by screens and fast-paced routines, it’s easy to forget the simple magic of a toy in…

6 months ago

Imagination Unboxed: Discover Joy at the Toy Shop in Delhi

In the heart of Delhi’s vibrant streets lies a world where imagination meets innovation — the magical universe of toys.…

6 months ago

Play with Purpose: Discovering the Ultimate Toy Store in Noida

When was the last time a toy truly amazed you—not just as a product, but as a thoughtful tool for…

6 months ago

From Tears to Toys: Exploring Modern Childhood through Delhi’s Favorite Toy Shop

In the digital age, the way we experience childhood has changed, but the essence remains the same—imagination, exploration, and joy.…

6 months ago