PHP str_repeat() Function

Usage — The PHP str_repeat() function is used to repeat a string given number of times.

It has the following syntax:


string str_repeat ( string $input , int $multiplier )

Here is an example of using str_repeat():


// Output — _/\__/\__/\__/\_
echo str_repeat("_/\_",4);

Return Value — This function returns a string in which the input string is repeated multiplier times.

PHP Version and Changelog — The str_repeat() function is available in PHP 4, PHP 5, PHP 7.

Relevant Functions — Other related PHP functions that you should know about are: str_pad() which pads a string to a certain length with another string and substr_count() which count the number of substring occurrences.

Go Directly To — usage, parameters, working examples or additional tips.



The input parameter is used to specify the string that needs to be repeated. This is a required parameter.


The multiplier parameter is used to specify the number of times the input string should be repeated. This is also a required parameter and its value must be greater than or equal to zero. If the multiplier is set to 0, the function will return an empty string.

Working Examples

Here are some examples of using the str_repeat() function:


// Output — ($_$)($_$)($_$)($_$)($_$)
echo str_repeat("(\$_\$)", 5);

Additional Tips

Here are some of the most upvoted tips taken from the comment section of the PHP manual:

  1. Here is a simple one liner to repeat a string multiple times with a separator:


    implode($separator, array_fill(0, $multiplier, $input));

    Example script:


    $input = 'bar';
    $multiplier = 5;
    $separator = ',';
    // Output — bar,bar,bar,bar,bar
    echo implode($separator, array_fill(0, $multiplier, $input));

    Suggested by - Damien Bezborodov

  2. I’ve faced this example :


    $my_head = str_repeat("°~", 35);
    echo $my_head;

    So , the length should be 35x2 = 70. However, if we echo it :


    $my_head = str_repeat("°~", 35);
    echo strlen($my_head); // 105
    echo mb_strlen($my_head, 'UTF-8'); // 70

    Be careful with characters and try to use mb_* package to make sure everything goes well …

    Suggested by - Anonymous

Further Reading

  1. You can read more about the PHP str_repeat() function on

Reader Comments

1. You can register or login to post a comment. Asking readers to register improves the quality of discussion.

2. As a registered user, you will also get to access features like choosing between a light and dark theme or anything else that we might implement in future.

Follow Us For Updates

Go To Top