The str_repeat
method returns a new string whose value is the concatenation of the argument string repeated n
times, where n
is passed as an argument.
str_repeat(string $string, int $n): string
string
: The string to be repeated.
n
: An integer value representing the number of times the string needs to be repeated. n
should be a positive value. If we pass 0
, then an empty string will return.
<?phpecho "loading". str_repeat(".", 10);?>
In the code above, we call the str_repeat
method with .
as the value for the 10
as the value for the .
is repeated 10
times.
<?phpecho str_repeat("^", 5);echo("\n");echo str_repeat("---", 0); // empty string will be returnedecho("\n");echo str_repeat("$$$", 1);echo("\n");echo str_repeat("Test ", 3);// echo str_repeat("Test ", -1); // n < 0 -> this will throw warning?>