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

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

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

Parameters

ParameterDescription
string The input string.
times How many times to repeat; must be 0 or more.

Return value

The repeated string.

Examples

Repeat textTry it yourself
<?php
echo str_repeat("ab", 3), "\n";
echo str_repeat("=", 20), "\n";

Output

ababab
====================
IndentationTry it yourself
<?php
$depth = 2;
echo str_repeat("  ", $depth) . "- nested item", "\n";

Output

    - nested item

Version history

VersionChange
PHP 4.0Added

In other languages

Was this page helpful?