str_repeat()
把字符串重复指定次数。
str_repeat(string $string, int $times): string
参数
| 参数 | 类型 | 说明 |
|---|---|---|
$string | string | 要重复的字符串 |
$times | int | 重复次数,必须大于等于 0 |
返回值
string — 重复后的字符串。
示例
echo str_repeat('ab', 3);
echo str_repeat('-', 20);输出
ababab--------------------
注意
- times 为 0 返回空字符串,负数会抛 ValueError(PHP 8)。用它做分隔线比值循环更清楚。