PHP str_repeat: How to repeat strings in PHP

PHP str_repeat() Function Example | str_repeat() Method In PHP

The str_repeat() function repeats the string the specified number of times.

PHP str_repeat()

PHP str_repeat() is a built-in function that repeats a string a specified number of times. The str_repeat() function takes the string and the integer as arguments. It returns the new string, which is generated by repeating a string passed as an argument by several times defined by an integer passed as an argument to this function.

The str_repeat() function creates the new string by repeating the given string a fixed number of times.


See the following syntax.

str_repeat(string, repeat)


The string parameter is required and specifies a string to repeat.

The repeat parameter is required, and it specifies the number of times the string will be repeated. Must be greater or equal to 0.


See the following code example.

  echo str_repeat("Eleven ", 11);

See the output.

➜  pro php Actor.php
Eleven Eleven Eleven Eleven Eleven Eleven Eleven Eleven Eleven Eleven Eleven    
➜  pro

The function returns the new string made up by repeating the given string $string given times.

If the parameter $repeat passed to the function equals 0, the function returns an empty string.

That’s it for this tutorial.

Recommended Posts

PHP join()

PHP class

PHP implode()

PHP str_replace()

PHP explode()

Leave A Reply

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.