Home > Article > Backend Development > PHP repeats a string a specified number of times with the function str_repeat()
Example
Repeat the string "." 13 times:
<?php echo str_repeat(".",13); ?>
Definition and usage
str_repeat() function repeats the string specified number of times.
Syntax
str_repeat(string,repeat)
Parameters | Description |
string | Required. Specifies the string to be repeated. |
repeat | Required. Specifies the number of times a string will be repeated. Must be greater than or equal to 0. |
Technical details
Return value: | Returns the repeated string. |
PHP version: | 4+ |
/*PHP版实现*/ function repeat($str, $num){ return implode( $str, array_fill(0, $num+1, '') ); }2:
JavaScriptImplementation:
/*JavaScript实现*/ function repeat(str, num){ return new Array( num + 1 ).join( str ); }
The above is the detailed content of PHP repeats a string a specified number of times with the function str_repeat(). For more information, please follow other related articles on the PHP Chinese website!