PHP str_repeat() Function
Last Updated :
21 Jun, 2023
Improve
The str_repeat() function is a built-in function in PHP and is used to create a new string by repeating a given string fixed number of times. It takes a string and an integer as arguments and returns a new string which is generated by repeating the string passed as an argument by the number of times defined by the integer passed as an argument to this function.
Syntax:
PHP
Output:
PHP
Output:
string str_repeat ( $string, $no_of_times )Parameters: This function accepts two parameters and both of them are mandatory to be passed.
- $string: This parameter represents the string to be repeated
- $no_of_times: This parameter represents a number denoting the number of times the parameter $string is to be repeated. This parameter should be greater than or equals to zero.
Input : $str = \'GeeksForGeeks\'; print_r(str_repeat($str, 2)); Output :GeeksForGeeksGeeksForGeeks Input : $str = \' Run\'; print_r(str_repeat($str, 7)); Output : Run Run Run Run Run Run RunBelow programs illustrate the str_repeat() function in PHP: Program - 1:
<?php
// Input string
$str = 'GeeksForGeeks';
// Repeated string
print_r(str_repeat($str, 2));
?>
GeeksForGeeksGeeksForGeeksProgram - 2:
<?php
// Input string
$str = ' Run';
// Repeated string
print_r(str_repeat($str, 7));
?>
Run Run Run Run Run Run RunReference: http://php.net/manual/en/function.str-repeat.php