PHP gives you many string tools. One of them is php str_repeat. This function repeats a texts as many times as you need. You can build patterns, repeat characters, or format output.
Table of Content
Understand the PHP str_repeat Function
You can use str_repeat()
to duplicate a string multiple times. You give it two things: the text you want and how many times you want to repeat it. PHP puts them together and gives you one string back.
You write it like this:
str_repeat(string $input, int $multiplier)
You must pass a text and an integer. The string is what you want to repeat. The number tells PHP how many times to do that.
$input
: the text you want to repeat. It can be one character or a full sentence.$multiplier
: how many times to repeat it. If this is 0 or less, PHP returns an empty string.
The return is one string. It contains your input repeated in a row.
Here is a quick example:
echo str_repeat("Hi ", 3);
The output:
Hi Hi Hi
PHP repeats “Hi ” three times and joins it into one line.
You can repeat anything. Here are a few examples:
echo str_repeat("-", 10); // ----------
echo str_repeat("ABC", 2); // ABCABC
echo str_repeat("0", 5); // 00000
You can use this to build spaces or fillers. Or also repeat patterns.
PHP str_repeat Output formatting using str_repeat()
You can also use str_repeat()
when you want a better layout or pattern control. It helps in output design or spacing.
You can repeat parts of a string and then pad it to a full length:
$credit_card = "5555666677775424";
$masked = str_repeat("*", 12) . substr($credit_card, -4);
echo $masked;
The output:
************5424
Let me also explain the logic:
str_repeat("*", 12)
generates 12 asterisks.substr($credit_card, -4)
grabs the last 4 digits.- Combine both to mask the first 12 digits.
You can build a line from an array and repeat spaces:
$items = ["A", "B", "C"];
$gap = str_repeat(" ", 3);
echo implode($gap, $items);
The output:
A B C
This joins items with three spaces between each.
Examples of str_repeat
Function in PHP
You can use user input or loop counters to decide how many times to repeat:
$count = 4;
for ($i = 1; $i <= $count; $i++) {
echo str_repeat("#", $i) . "\n";
}
This creates a step pattern:
#
##
###
####
You can use symbols, emojis, or non-English letters:
echo str_repeat("🔥", 3); // 🔥🔥🔥
echo str_repeat("ü", 4); // üüüü
It handles UTF-8 strings without trouble.
You can repeat HTML tags or spaces to build templates:
echo "<ul>";
echo str_repeat("<li>Item</li>", 3);
echo "</ul>";
This creates three list items inside a <ul>
block.
When you want fake rows or spacer lines in CLI or reports:
echo str_repeat("-", 50);
// Output: --------------------------------------------------
You can also use it to create placeholder strings or block letters in fake text.
Wrapping Up
In this article, you learned how to use php str_repeat
in plain text and inside loops. You also saw how to combine it with implode
, str_pad
or HTML elements.
Here is a quick recap:
str_repeat()
repeats a text based on a count.- It works with plain text and symbols with HTML.
- You can use it with loops and arrays. And even user input.
- It helps with output formatting and build patterns.
FAQs
What is the use of str_repeat in PHP?
What happens if I give a negative number?
Can I repeat Unicode or emoji?
Can I use it with user input?
Similar Reads
PHP introduced traits to solve a problem with code reuse. Before traits, developers used inheritance and interfaces to share functionality…
You can use your Raspberry Pi as a personal web server or tool that runs in your home. You do…
You have two or more arrays. You want one. That is the problem array_merge solves in PHP. This function lets…
PHP static property helps you manage shared data across instances, but can lead to hidden state changes. In this article,…
Escape characters appeared in PHP because some symbols in strings serve special purposes. For example, a quote can end a…
Programming is all about choices. Everything you do in code boils down to answering a question: What should happen if…
Understanding PHP syntax is like the basics of any language—PHP syntax defines the rules for writing code that a server…
Connecting PHP to MySQL is not just a technical step—it is the basic part of how data-driven websites work. From…
The PHP compact function is a way to pull variables into an array. In this article, you will learn how…
You may need to get part of a text when you work with text in PHP. The mb_substr gives you…