PHP str_pad to print string patterns Last Updated : 01 Apr, 2018 Summarize Comments Improve Suggest changes Share Like Article Like Report str_pad: Pad a string to a certain length with another string. Syntax:- str_pad (input, pad_length, pad_string_value, pad_type) It returns the padded string. Parameters Description input:-The input string. pad_length:-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 is returned. pad_string_input:-The pad_string may be truncated if the required number of padding characters can't be evenly divided by the pad_string's length. pad_type:-Optional argument pad_type can be STR_PAD_RIGHT, STR_PAD_LEFT, or STR_PAD_BOTH. If pad_type is not specified it is assumed to be STR_PAD_RIGHT. Print simple patterns like below using single line of code under loop. Examples: Input : 5 Output : * ** *** **** ***** Input : 6 Output : * ** *** **** ***** ****** php <?php // PHP program to print a pattern using only // one loop. function generatePattern($n) { // Initialize the string $str = ""; // Iterate for n lines for ($i=0 ; $i<$n ; $i++) { // Concatenate the string $str .= "*"; echo str_pad($str, $n, " ", STR_PAD_LEFT), "\n"; } } // Driver code $n = 6; generatePattern($n); ?> Output: * ** *** **** ***** ****** Comment More infoAdvertise with us Next Article PHP strpos() and stripos() Functions M Mithun Kumar Follow Improve Article Tags : PHP PHP-string Similar Reads How to read each character of a string in PHP ? A string is a sequence of characters. It may contain integers or even special symbols. Every character in a string is stored at a unique position represented by a unique index value. Here are some approaches to read each character of a string in PHPTable of ContentUsing str_split() method - The str_ 4 min read How to get the last character of a string in PHP ? In this article, we will find the last character of a string in PHP. The last character can be found using the following methods.Using array() Method: In this method, we will find the length of the string, then print the value of (length-1). For example, if the string is "Akshit" Its length is 6, in 2 min read PHP strpos() and stripos() Functions In this article, we will see how to find the position of the first occurrence of a string in another string using strpos() and stripos() Functions in PHP, & will see their implementation through the examples. Both the strpos() and stripos() Functions in PHP are binary-safe which means the functi 4 min read How to replace String in PHP ? Replacing a string in PHP involves substituting parts of a string with another string. Common methods include str_replace() for simple replacements, preg_replace() for pattern-based replacements, substr_replace() for positional replacements, and str_ireplace() for case-insensitive replacements. Each 2 min read How to replace multiple characters in a string in PHP ? A string is a sequence of characters enclosed within single or double quotes. A string can also be looped through and modifications can be made to replace a particular sequence of characters in it. In this article, we will see how to replace multiple characters in a string in PHP.Using the str_repla 3 min read How to get the position of character in a string in PHP ? In this article, we will get the position of the character in the given string in PHP. String is a set of characters. We will get the position of the character in a string by using strpos() function. Syntax: strpos(string, character, start_pos) Parameters: string (mandatory): This parameter refers t 2 min read Like