Home »
PHP
PHP str_repeat() Function with Example
By IncludeHelp Last updated : December 27, 2023
PHP str_repeat() Function
The str_repeat() function is a string function in PHP and it is used to repeat a given string a given number of times and returns the string.
Syntax
The syntax of the str_repeat() function:
str_repeat(string, n);
Parameters
The parameters of the str_repeat() function:
- string is the source string to be repeated.
- n defines the number of repetition on the string.
Return Value
The return value of this method is string, it returns string repeated times times.
Sample Input/Output
Input:
str = "Hello"
n = 3
Output:
"HelloHelloHello"
Input:
str = "Okay "
n = 4
Output:
"Okay Okay Okay Okay"
Example of PHP str_repeat() Function
<?php
$str = "Hello";
$output = str_repeat($str,3);
echo ($output."\n");
$str = "Okay ";
$output = str_repeat($str,4);
echo ($output."\n");
$str = "...!";
$output = str_repeat($str,5);
echo ($output."\n");
?>
Output
The output of the above example is:
HelloHelloHello
Okay Okay Okay Okay
...!...!...!...!...!
To understand the above example, you should have the basic knowledge of the following PHP topics: