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

黄舟
Release: 2023-03-17 06:02:01
Original
2070 people have browsed it

Example

Repeat thestring"." 13 times:

Copy after login

Definition and usage

str_repeat() function repeats the string specified number of times.

Syntax

str_repeat(string,repeat)
Copy after login
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, '') ); }
Copy after login

2:

JavaScriptImplementation:

/*JavaScript实现*/ function repeat(str, num){ return new Array( num + 1 ).join( str ); }
Copy after login


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!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!