Using PHP str_repeat() Function: Syntax, Parameters, and Practical Examples
This article explains the PHP str_repeat() function, covering its basic syntax, parameters, return value, and multiple practical examples such as creating separators, padding strings, generating patterns, and producing random strings, while providing complete code snippets and output demonstrations.
Basic Usage of str_repeat()
The str_repeat() function in PHP repeats a given string a specified number of times.
Syntax
string str_repeat ( string $input , int $multiplier )Parameters
$input: the string to be repeated. $multiplier: the number of repetitions.
Return Value
Returns the input string repeated the specified number of times.
Example
A simple example demonstrating how to repeat a string:
$input = "Hello ";
$multiplier = 3;
$result = str_repeat($input, $multiplier);
echo $result;Output:
Hello Hello HelloAdvanced Usage of str_repeat()
Beyond basic repetition, str_repeat() can be combined with other string functions to achieve more complex tasks.
1. Generate Separator Line
Use str_repeat() to create a line of repeated characters, such as a series of dashes:
$separator = str_repeat("-", 50);
echo $separator;Output:
--------------------------------------------------2. Pad Strings
Combine str_repeat() with spaces to left‑align or right‑align a string:
$text = "Hello";
$padding = str_repeat(" ", 10 - strlen($text));
$leftAligned = $text . $padding;
$rightAligned = $padding . $text;
echo $leftAligned . "
";
echo $rightAligned . "
";Output:
Hello
Hello3. Generate Repeating Patterns
Create a triangle pattern using a loop and str_repeat():
$height = 5;
for ($i = 1; $i <= $height; $i++) {
$line = str_repeat("*", $i);
echo $line . "
";
}Output:
*
**
***
****
*****4. Generate Random Strings
Combine str_repeat() with str_shuffle() and substr() to produce a random string of a given length:
$length = 10;
$characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$randomString = str_repeat($characters, $length);
echo substr(str_shuffle($randomString), 0, $length);Sample output (will vary):
2C7X5R3z4xSummary
The article provides a detailed overview of PHP's str_repeat() function, including its basic usage, parameters, return value, and a variety of practical examples such as creating separator lines, padding strings, generating repeating patterns, and producing random strings. Mastering this function enhances string manipulation efficiency and flexibility in PHP development.
Recommended PHP Learning Resources
Vue3+Laravel8+Uniapp Beginner to Project Development Tutorial
Vue3+TP6+API Social E‑commerce System Development Course
Swoole From Beginner to Master Course
Workerman+TP6 Real‑time Chat System (Limited Offer)
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
php Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
