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
If you've ever tried to pull data from an XML file, you know it can be a bit tricky at…
Array values may look the same, but keys can differ. The array_diff_assoc in PHP finds differences by both values and…
JSON is used in most of your web applications for dealing with data, and so, in a way, PHP JSON…
If you are working with PHP and MySQL, one of the first tasks you may need is to create a…
You always start with “Hello World” when you learn a new language. It keeps things simple. You see right away…
Bitwise operators use symbols like &, |, and ^, and they work at the binary level. But once you understand…
You may need to repeat tasks in PHP. The PHP for loop solves this by letting you run code many…
Inserting multiple data rows using PHP is a basic task. However when you start working with MySQL databases, So that…
Filtering data is a big part of getting the right information to show up. That is where the WHERE clause…
PHP mail() is a great built-in function for sending an email directly from your script for account confirmations, password resets, or sending notifications.…