PHP str_pad function


PHP str_pad function : Is used to pad a string to new length with another string. This function basically pads the string to the left, right or both side of the string with a string. It accepts the string as input parameter and returns the padded string with new length. We are going to explain the str_pad function with example and demo.


PHP str_pad function Syntax

The Syntax for the str_pad is given below-

PHP str_pad function Syntax

str_pad(string,length,pad_str,pad_type);

Parameters

The Input parameters are –

  • string : String to be padded
  • length : New length of string. It should be more than the original length of the string.
  • pad_str : It’s optional. This is string to be added as pad. If you do not add this option by default whitespace will be added.
  • pad_type :It’s optional. This decides which side to add pad – Left, Right or Both Side. Available options are –

    • STR_PAD_BOTH : Add pad to both sides.


    • STR_PAD_LEFT : Add pad to left side.


    • STR_PAD_RIGHT : Add pad to right side.

Output

Returns the padded string.

Technical Requirements

Technical requirements are –

PHP Version : 4.01

PHP str_pad function Example

Here is very basic example of str_pad function-

PHP str_pad function Example:

$string = "Welcome to Tutorialsplane.";
$paddedString = str_pad($string,30,'*',STR_PAD_RIGHT);
echo $paddedString;

Try it »

If you run the above example it will produce output like this –

PHP str_pad function Example


Advertisements

Add Comment

📖 Read More