Published May 30, 2023Updated Jun 22, 2023
Contribute to Docs

The str_repeat() function returns a string that repeats a substring a specified number of times.


str_repeat($string, $repeat)

The str_repeat() function has two required parameters:

  • $string: Specifies the string to repeat.
  • $repeat: Specifies the number of times $string will be repeated. It must be greater or equal to 0.

The str_repeat() function returns a string containing the given $string repeated $repeat times. If $repeat is 0, it returns an empty string.


The following example uses the str_repeat() function to repeat the "bon" 2 times. Then the echo command prints this string to the console:

echo str_repeat("bon", 2);

The example will result in the following output:


Codebyte Example

This example is runnable and uses the str_repeat() function:

Note: The third echo statement will not output to the console due to the second argument of str_repeat being set to 0.


All contributors

Looking to contribute?

Learn PHP on Codecademy