PHP | str_repeat() Function
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:
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.
Return Value: This function returns a new string made up by repeating the given string $string given number of times. If the parameter $no_of_times passed to the function is equals to 0, then the function returns an empty string.
Examples:
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 Run
Below programs illustrate the str_repeat() function in PHP:
Program – 1:
<?php // Input string $str = 'GeeksForGeeks'; // Repeated string print_r(str_repeat($str, 2)); ?> |
Output:
GeeksForGeeksGeeksForGeeks
Program – 2:
<?php // Input string $str = ' Run'; // Repeated string print_r(str_repeat($str, 7)); ?> |
Output:
Run Run Run Run Run Run Run
Reference:
http://php.net/manual/en/function.str-repeat.php
Recommended Posts:
- How to call a function that return another function in JavaScript ?
- How to get the function name from within that function using JavaScript ?
- p5.js | hex() function
- PHP | pi( ) Function
- D3.js | d3.max() function
- p5.js | str() function
- PHP | Ds\Map map() Function
- PHP | Ds\Map last() Function
- p5.js | box() Function
- p5.js | arc() Function
- PHP | pow( ) Function
- PHP | Ds\Set last() Function
- PHP | Ds\Set first() Function
- p5.js | sin() function
- PHP | Ds\Set add() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



