echo str_repeat('$',10);
The above command will print $ ten times.
$$$$$$$$$$
Syntax.
str_repeat (input $string, int $multiplier );
Parameter | DESCRIPTION |
$string | Required : Input string which is to be repeated |
$multiplier | Required : Integer , Number of times the input string is to be repeated. |
Using str_repeat string function we can repeat a string by a number ( known as multiplier ).
We will generate this pattern ?
*
**
***
****
*****
******
*******
********
*********
**********
Here is the code for this
for($i=1;$i<=10;$i++){
echo str_repeat("*",$i);
echo "<br>";
}
Example: Repeat String with Custom Separator
$separator = str_repeat('-=', 5);
echo $separator; // Output: -=-=-=-=-=-=
Example: Creating a Line Divider
$divider = str_repeat('-', 50);
echo $divider; // Output: --------------------------------------------------
Example: Padding a String to a Specific Length
$str = 'Hello';
$padded_str = str_repeat(' ', 10) . $str;
echo $padded_str; // Output: " Hello"
Example: Building a Simple Progress Bar
$progress = 7; // Progress out of 10
$bar = str_repeat('=', $progress) . str_repeat('-', 10 - $progress);
echo "[$bar]"; // Output: [=======---]
Example: Generating HTML List Items
$list_item = str_repeat('Item', 5);
echo "";
// Output:
Example: Repeating Patterns in a Password
$password = str_repeat('ab1!', 3);
echo $password; // Output: ab1!ab1!ab1!
← String Functions str_pad(): Padding string →
nl2br() To add Line break →
← Subscribe to our YouTube Channel here