PHP Function Reference

PHP str_repeat() Function



The PHP str_repeat() function is used to repeat the given string by specified number of times. The function has an required argument which is used to specify the number of repetition.

Syntax

str_repeat(string, repeat)

Parameters

string Required. Specify the string to be repeated.
repeat Required. Specify the number of times the string will be repeated. It has to be greater than or equal to 0. If this is set to 0, the function will return an empty string.

Return Value

Returns the repeated version of the specified string.

Example:

In the example below, str_repeat() function is used to repeat the given string by five times.

<?php
$MyString = "Hi";
$NewString = str_repeat($MyString, 5);

echo $NewString."\n";
?>

The output of the above code will be:

HiHiHiHiHi

❮ PHP String Reference