简体   繁体   中英

How to create a string with x numbers of the same character without looping in PHP

Is there a nice way (without looping) to create a string constructed by a number of the same characters?

Eg I know I should add 5 '.' to the beginning of a string. How do I do that without looping?

I don't know beforehand that it should be 5 '.', that can vary. Hence my question.

I'm using PHP.

尝试使用str_repeat

$output = str_repeat(".", 5); // .....

Use the str_repeat() function. For example, to concatenate 5 dots together without using a loop, you could do this:

$fiveDots = str_repeat('.', 5);

使用str_pad()函数......就这么简单。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM