PHP str_pad() Function Last Updated : 21 Jun, 2023 Comments Improve Suggest changes Like Article Like Report 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 input string will be padded by spaces. Syntax : string str_pad($string, $length, $pad_string, $pad_type) Parameters: This function accepts four parameters as shown in the above syntax out of which first two are mandatory to be supplied and rest two are optional. All of these parameters are described below: $string: This parameter is mandatory. It specifies the input string which is needed to be padded. $length: This parameter is also mandatory. It specifies the length of the new string that will be generated after padding the input string $string. If this length is less than or equals the length of input string then no padding will be done. $pad_string: This parameter is optional and its default value is whitespace ' '. It specifies the string to be used for padding. $pad_type: This parameter is also optional. It specifies which side of the string needs to be padded, i.e. left, right or both. By default it's value is set to STR_PAD_RIGHT. If we want to pad the left side of the input string then we should set this parameter to STR_PAD_LEFT and if we want to pad both sides then this parameter should be set to STR_PAD_BOTH. Return Value: This parameter returns a new string obtained after padding the input string $string. Examples: Input : $string = "Hello World", $length = 20, $pad_string = "." Output : Hello World........ Input : $string = "Geeks for geeks", $length = 18, $pad_string = ")" Output : Geeks for geeks))) Below programs illustrate the str_pad() function in PHP: Program 1: In this program we will pad to both the sides of the input string by setting last parameter to STR_PAD_BOTH. If the padding length is not an even number, the right side gets the extra padding. php <?php $str = "Geeks for geeks"; echo str_pad($str, 21, ":-)", STR_PAD_BOTH); ?> Output: :-)Geeks for geeks:-) Program 2: In this program we will pad to left side of the input string by setting last parameter to STR_PAD_LEFT. php <?php $str = "Geeks for geeks"; echo str_pad($str, 25, "Contribute", STR_PAD_LEFT); ?> Output: ContributeGeeks for geeks Program 3: In this program we will pad to right side of the input string by setting last parameter to STR_PAD_RIGHT. php <?php $str = "Geeks for geeks"; echo str_pad($str, 26, " Contribute", STR_PAD_RIGHT); ?> Output: Geeks for geeks Contribute Reference: http://php.net/manual/en/function.str-pad.php Comment More infoAdvertise with us Next Article PHP str_pad() Function R RICHIK BHATTACHARJEE Follow Improve Article Tags : Misc Web Technologies PHP PHP-string Practice Tags : Misc Similar Reads PHP addcslashes() Function The addcslashes() function is a built-in function in PHP. The addcslashes() function is used to add backslashes before some specified characters in a given string. Syntax: string addcslashes($string, $characters) Parameters: This function accepts two parameters as shown in the above syntax and are d 2 min read PHP addslashes() Function The addslashes() function is an inbuilt function in PHP and it returns a string with backslashes in front of predefined characters. It does not take any specified characters in the parameter. The predefined characters are: single quote (')double quote (")backslash (\)NULL Note: The addslashes() func 2 min read PHP bin2hex() Function The bin2hex() function in PHP converts a string to hexadecimal values. The conversion is done byte-wise with the high-nibble first. Note: It is not for converting strings representing binary digits into hexadecimal. Syntax: bin2hex($string) Parameters: This function accepts a single parameter $strin 1 min read PHP chop() Function The chop() in PHP is used to remove white spaces or any other specified characters from the end of a string. Syntax: string chop($string, $character) Parameters: This function accepts two parameters as shown in the above syntax and are described below: $string : It is used to specify the string whic 2 min read PHP chr() Function The chr() function is a built-in function in PHP and is used to convert a ASCII value to a character. It accepts an ASCII value as a parameter and returns a string representing a character from the specified ASCII value. The ASCII value can be specified in decimal, octal, or hex values. Octal values 2 min read PHP chunk_split() Function The chunk_split() function is a built-in function in PHP. The chunk_split() function is used to split a string into smaller chunks of a specific length. Syntax: string chunk_split($string, $length, $end) Parameters: This function accepts three parameters as shown in the above syntax and are describe 2 min read PHP convert_uudecode() Function The convert_uudecode() is a built in function in PHP. This function decode a uuencoded string encoded using convert_uuencode() function. The uudecode() functions makes string into printable form. Syntax: string convert_uudecode(string) Parameters: The uuencoded string which will be decoded. Return T 1 min read PHP convert_uuencode() Function The convert_uuencode() is a built-in function in PHP. The convert_uuencode() function encodes a string using the uuencode algorithm. Uuencode encoding translates all strings (including binary data) into printable characters which makes them safe for network transmissions. Syntax: String convert_uuen 1 min read PHP count_chars() Function The count_chars() is an inbuilt function in PHP and is used to perform several operations related to string like the number of an ASCII character occurs in a string. Syntax : count_chars(string,return_mode); Parameters: The count_chars() function takes two parameters string and return_mode as explai 2 min read PHP crc32() Function The crc32() function helps us to calculate a 32-bit crc or cyclic redundancy checksum polynomial for a string. The function uses the CRC32 algorithm.This function can be used to validate data integrity. However, to ensure that we get the correct string representation from the crc32() function, we ne 2 min read Like