PHP str_pad() Function

PHP str_pad() Function

The str_pad() function in PHP pads a string to the given length with another string.

Syntax

str_pad ( string $input , int $pad_length [, string $pad_string  [, int $pad_type ]] )

Parameter

input(required)- This parameter signifies the input string.

pad_length(required)- This parameter specifies the length of the new string. If this value is less than the original length of the string, nothing will be done.

pad_string(optional)- This parameter specifies the string to use for padding. The default value is whitespace.

pad_type(optional)- This parameter specifies what side to pad the string.

The possible pad_type values are as follows:

  • STR_PAD_BOTH - Pad to both sides of the string. If not an even number, the right side gets the extra padding
  • STR_PAD_LEFT - Pad to the left side of the string
  • STR_PAD_RIGHT - Pad to the right side of the string and this is the default value.

Return

This function returns the padded string.

Example 1

   

Output

The actual string: Hello World
 By using str_pad() function
 New String: Hello World....   

Example 2

   

Output

The actual string: Hello World
 By using str_pad() function
 New String: ....Hello World   

Example 3

   

Output

The actual string: Hello World
 By using str_pad() function
 New String: ..Hello World..   

Example 4

   

Output

The actual string: Hello World
By using str_pad() function
New String: Hello World   

Related Topics

PHP is_numeric() Function

The is_numeric() function in PHP finds whether the specified variable is a number or a numeric string. It returns a Boolean value TRUE if the specified var is a number or a numeric string else it returns FALSE otherwise. Syntax is_numeric ( mixed...

1 minute read.

PHP sha1_file() Function

PHP sha1_file() Function The sha1_file() in PHP calculates the sha1 hash of a file and returns the hash (40-character hexadecimal number). Syntax sha1_file ( string $filename [, bool $raw_output] )  Parameter filename(required)- This parameter represents the filename of the file to hash. raw_output(optional)- This...

1 minute read.

PHP array_keys() Function

PHP array_keys() Function The array_keys() function  in PHP is used to return all the keys, numeric and string from the given array. Syntax array_keys ( array $array ) or array_keys ( array $array , mixed $search_value [, bool $strict = FALSE ] )  Parameter array(required)- This parameter specifies the input array search_value(optional)- This parameter specifies a value, where...

1 minute read.

PHP array_diff_uassoc() Function

PHP array_diff_uassoc() Function The array_diff_uassoc () Function in PHP is used to compare the keys and values of two (or more) arrays and returns the differences (an array containing the entries from the first array that...

1 minute read.

PHP Switch Statement with Example

PHP switch statement The PHP switch statement is used to execute one condition from many conditions.It similar to if statements on the same expression. Syntax: switch(n) {             case1: statement if n=case1; break;             case2:             statement if n=case2; break;             case3:            ...

3 minutes read.

PHP implode() Function

PHP implode() Function The implode() function in PHP joins the specified array elements with a string. Syntax implode ( string $glue, array $pieces ) Parameter glue(optional)- It specifies what to put between the array elements. pieces(required)- This parameter represents...

1 minute read.

PHP metaphone() Function

PHP metaphone() Function The metaphone() function in PHP calculates the metaphone key (representing how a string sounds or pronounced) of a string. This function is used to text search and text matching or spelling...

1 minute read.

PHP strcasecmp() Function

PHP strcasecmp() Function The strcasecmp() function in PHP compares two strings. It is a binary-safe case-insensitive string function. Syntax strcasecmp ( string $str1 , string $str2 ) Parameter str1(required)- This parameter represents the first string. str2(required)- This parameter represents the second string. Return This function returns a value less than( <...

1 minute read.

PHP Include Function

PHP Include( ) The PHP include() statement holds all the code/text that liesin the defined file and paste it into the file that uses the "include" statement. With the help of include...

3 minutes read.

PHP stripcslashes() Function

PHP stripcslashes() Function The stripcslashes() function in PHP returns a string with backslashes(\n, \r ..., octal and hexadecimal representation) stripped off. Syntax stripcslashes ( string $str )  Parameter str(required)- This parameter signifies the string to be unescaped. Return This function returns the unescaped string with backslashes stripped off. Example 1   ...

1 minute read.

PHP array_uintersect() Function

PHP array_uintersect() Function The array_uintersect() function in PHP calculates the intersection of arrays by comparing the data by a callback function. It returns an array containing all the values of the first input array that are...

2 minutes read.

PHP is_scalar() Function

PHP is_scalar() Function The is_scalar() function in PHP finds whether a variable is a scalar or not. It returns a Boolean value TRUE if the parameter var is scalar, else it returns FALSE. Scalar variables are those containing an integer, float, string or boolean. This...

1 minute read.

PHP current() function

PHP current() function The current() function in PHP returns the current element in an array, which is initialized to the first element inserted into the array. Syntax current ( array $array )   Parameter array (required)– This parameter represents the input array or countable object. Return This...

1 minute read.

PHP strlen() Function

PHP strlen() Function The strlen() function in PHP is used to return the length of the specified string. Syntax strlen ( string $string ) Parameter string(required)- This parameter represents the string which is measured for length. Return This function returns the length of the string on success or returns 0 if the specified...

1 minute read.

PHP compact() function

PHP compact() function The compact() function in PHP is used to create an array containing variables and their values. It returns the output array whose keys are the variable names, and their corresponding values are...

1 minute read.

PHP is_array() Function

The is_array() Function in PHP is used to  find whether a variable is an array or not. It returns a Boolean value true if the parameter var is an array else it returns...

1 minute read.

PHP Date and Time

DATE: The PHP date() function formats a date or time. The timestamp is used to format a more readable date and time into a PHP date () function. Syntax: date(format,timestamp) Simple Date: Characters that...

1 minute read.

PHP is_double() Function

The PHP is_double() function in PHP is used to  find whether the type of the specified variable is a float or not. It returns a Boolean value true if the parameter var is...

1 minute read.

PHP ucfirst() Function

PHP ucfirst() Function The ucfirst() function in PHP converts the first character of the specified string to uppercase. Syntax ucfirst ( string $str ) Parameter str(required)- This parameter represents the input string. Return This function returns a string with the first character of the specified string capitalized if that...

1 minute read.

PHP Indexed Array

In PHP, arrays are linear data structures that allow to store multiple elements of the same type under a single variable which helps in saving the time and effort of...

3 minutes read.