Home  >  Article  >  Backend Development  >  PHP repeats a string a specified number of times with the function str_repeat()

PHP repeats a string a specified number of times with the function str_repeat()

黄舟
黄舟Original
2017-11-03 11:36:372131browse

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+
##There is a function in PHP: String str_repeat($str, num); is very easy to use.... The following is simulated through js and php.

1:PHP version

/*PHP版实现*/ 
function repeat($str, $num){ 
return implode( $str, array_fill(0, $num+1, &#39;&#39;) ); 
}

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn