PHP 8 reference · String Functions
PHP str_repeat() Function
Repeat a string a given number of times and return the result. A count of 0 returns an empty string.
PHP 8 Since 4.0
Syntax
str_repeat(string $string, int $times): string
Parameters
| Parameter | Description |
|---|---|
string |
The input string. |
times |
How many times to repeat; must be 0 or more. |
Return value
The repeated string.
Examples
<?php
echo str_repeat("ab", 3), "\n";
echo str_repeat("=", 20), "\n";
Output
ababab ====================
<?php
$depth = 2;
echo str_repeat(" ", $depth) . "- nested item", "\n";
Output
- nested item
Version history
| Version | Change |
|---|---|
| PHP 4.0 | Added |
In other languages
string.repeat()· JavaScript
Related
str_pad()· Pad a string to a given length with spaces or another string, on the right, left or both sides.implode()· Join array elements into one string with a separator between them. join() is an alias of implode().- PHP Strings · tutorial