Table of Content
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.
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); // 00000You 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
One of the first tasks you will likely encounter is creating a table in databases. Tables are the main part…
PHP-type hinting is something that keeps your code in check and helps your functions receive just the right types of…
PHP array_multisort sorts many arrays or a multi level array in one go. It can sort data in numeric order…
If you are working with PHP and MySQL, one of the first tasks you may need is to create a…
PHP 8.4 released the array_any to test if at least one element matches a condition. Understand the array_any Function in…
Inserting documents into your MongoDB collections is one of the most basic but important tasks when working with PHP. In…
You may need to repeat tasks in PHP. The PHP for loop solves this by letting you run code many…
Before PHP 5.2, there was no built-in filter extension in PHP. You had to manually handle and sanitize input. PHP…
The array_combine function in PHP creates a new array with one array for keys and another for values. It needs…
The PHP OR operator has two renditions: either || or or. Both of these are useful logical operators when you want to introduce…