PHP str_repeat()
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.
Syntax
str_repeat($string, $repeat)
The str_repeat() function has two required parameters:
$string: Specifies thestringto repeat.$repeat: Specifies the number of times$stringwill be repeated. It must be greater or equal to0.
The str_repeat() function returns a string containing the given $string repeated $repeat times. If $repeat is 0, it returns an empty string.
Example
The following example uses the str_repeat() function to repeat the "bon" 2 times. Then the echo command prints this string to the console:
<?phpecho str_repeat("bon", 2);?>
The example will result in the following output:
bonbon
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_repeatbeing set to0.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn PHP on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours