When you need to repeat a text fragment a given number of times, it helps to understand both the normal result and what changes with different inputs. This guide focuses on practical examples rather than repeating PHP background material.
str_repeat provides the behavior described below. The function returns a new string. A repetition count of zero returns an empty string.
What Is str_repeat in PHP?
Repeat a text fragment a given number of times. The function returns a new string. A repetition count of zero returns an empty string.
Its syntax is:
str_repeat(string $string, int $times): string
Parameters and return value: Read the argument names and optional values in the signature above. Methods use an existing object, while stand-alone functions take direct arguments. The examples demonstrate the relevant arguments and the value returned or the object modified.
Important: Negative counts raise ValueError and very large counts can consume excessive memory.
Example 1: Basic Usage
This first example demonstrates how to repeat a text fragment a given number of times with a small input.
<?php
echo str_repeat("ab", 3);
Output:
ababab
The example uses the simplest useful form. The function returns a new string. A repetition count of zero returns an empty string. The output may depend on the active ICU locale, PHP configuration, or input supplied to the function.
Example 2: Another Case
Here the input or option changes, making it easier to see where the behavior differs. Negative counts raise ValueError and very large counts can consume excessive memory.
<?php
echo "Title" . PHP_EOL . str_repeat("-", 5);
Output:
Title
-----
Compare the result to the first example. str_pad() targets a total width rather than a number of repetitions.
Practical Example: Applying the Function
This standalone example shows how the same operation fits into a small application task.
<?php
$levels = [1, 2, 4];
foreach ($levels as $level) {
echo str_repeat(" ", $level) . "Item" . PHP_EOL;
}
Output:
Item
Item
Item
The example keeps the operation visible and gives you a starting point for using it in an application. Negative counts raise ValueError and very large counts can consume excessive memory.
Important Behavior and Edge Cases
- Core behavior: The function returns a new string. A repetition count of zero returns an empty string.
- Return value: Check the result separately from changes to passed-in arrays, iterators, or object state.
- Data constraints: Negative counts raise ValueError and very large counts can consume excessive memory.
- Environment: Run this example under a PHP version supporting the API; check results and exceptions before using them.
Comparison With Related Functions
| Function or Approach | When to Use It |
|---|---|
str_repeat | Repeat a text fragment a given number of times. |
| Related PHP APIs | str_pad() targets a total width rather than a number of repetitions. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Assuming the function works on every kind of input | Follow the declared argument types and check the input format before calling it. |
| Ignoring the behavior of the result | Negative counts raise ValueError and very large counts can consume excessive memory. |
| Using the wrong related PHP API | str_pad() targets a total width rather than a number of repetitions. |
Key Takeaways
str_repeathelps you repeat a text fragment a given number of times.- The function returns a new string. A repetition count of zero returns an empty string.
- Negative counts raise ValueError and very large counts can consume excessive memory.
- str_pad() targets a total width rather than a number of repetitions.
str_repeat is useful when you need to repeat a text fragment a given number of times. Start with the small examples, then adapt the practical example while respecting its input and environment requirements.