The php str_repeat() function helps to repeat the string the specified number of times.
The str_repeat() function generates a new string by iterating over the given string a predetermined number of times.
PHP str_repeat()
PHP has a built-in function called str_repeat()
that repeats a string a specified number of times. The str_repeat()
function requires two arguments: a string and an integer. It produces a new string that is created by iterating through a string supplied as an input as many times as the integer passed as an argument to this function allows.
Syntax:
str_repeat(string, repeat)
Whereas:
– string : The string parameter is mandatory, and it specifies a repeating string.
– repeat : It specifies the number of times the string will be repeated. The value must be greater or equal to 0.
If the function’s $repeat
parameter is set to 0
, the function returns an empty string.
How To Repeat String in PHP
Let’s repeat a string in php.
echo str_repeat("phpflow ", 4);
Output:
phpflow phpflow phpflow phpflow
The function returns a new string created by iterating through the specified string $string.