PHP str_repeat: How to repeat strings 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.

Syntax

See the following syntax.

str_repeat(string, repeat)

Arguments

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.

Example

See the following code example.

<?php
  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 Comment

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