Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

str_repeat

Repeats a string a given number of times.

Available since: PHP 4

Syntax

str_repeat(string $string, int $times): string

Parameters

ParameterTypeDescription
$stringstringThe string to repeat.
$timesintHow 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:

ababab

A 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.

Thanks for reading!

Donate to the site — we work hard for you.