str_repeat
Repeats a string a given number of times.
Available since: PHP 4
Syntax
str_repeat(string $string, int $times): stringParameters
| Parameter | Type | Description |
|---|---|---|
| $string | string | The string to repeat. |
| $times | int | How many times to repeat. Must not be negative. |
Return value
stringThe string repeated `$times` times.
Examples
Repeating a fragment:
<?php
echo str_repeat('ab', 3);Output:
abababA separator line in text output:
<?php
echo str_repeat('-', 20);Output:
--------------------Notes
When $times is zero, an empty string is returned. A negative value throws an error.
See also
- implode — Joins array elements into a string.
Related recipes
In the tutorial
- Strings — Working with strings in PHP — quotes, concatenation, length, searching, replacing and multibyte functions.