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; |
If you run the above example it will produce output like this –
Advertisements
Add Comment
📖 Read More
- 1. PHP Check that variable is an array
- 2. Warning: Invalid argument supplied for foreach() in Php
- 3. PHP Objects
- 4. PHP Sort Array without using built in function