PHP Tutorial

PHP Flow Control

PHP Functions

PHP String

PHP Array

PHP Date Time

PHP Object Oriented

Regular Expression

PHP Cookie & Session

PHP Error & Exception handling

MySQL in PHP

PHP File Directory

PHP Image Processing

PHP Str_repeat(): Repeat A String

The str_repeat() function in PHP is used to repeat a string a specified number of times.

Here's the basic syntax of the str_repeat() function:

str_repeat(string, times)
  • string: The input string which you want to repeat.
  • times: The number of times you want to repeat the string.

Let's look at an example:

echo str_repeat("Hello ", 3); 
// Outputs: "Hello Hello Hello "

In this example, the string "Hello " is repeated 3 times.

This function is especially useful when you want to generate repeated patterns in a string, like creating a line of dashes for a separator:

echo str_repeat("-", 50); 
// Outputs: "--------------------------------------------------"

Or repeating a block of HTML code a certain number of times:

echo str_repeat("<br />", 10); 
// Outputs 10 line breaks

Note that str_repeat() does not add any separators between the repeated strings, so if you want something like a comma between each repeated string, you need to include it in the string itself, like "Hello, ".

If you provide a non-positive number for times, str_repeat() will return an empty string. If you provide a large number, be aware that str_repeat() can consume a lot of memory, and you may hit the memory limit of your PHP script.

  1. Repeat a string in PHP with str_repeat():

    The str_repeat() function in PHP is used to repeat a string a specified number of times. Here's an example:

    <?php
    $originalString = "Hello, ";
    $repeatedString = str_repeat($originalString, 3);
    
    echo $repeatedString; // Outputs: Hello, Hello, Hello,
    ?>
    
  2. PHP str_repeat() example code:

    Here's a simple example of using str_repeat() in PHP:

    <?php
    $pattern = "*";
    $repeatedPattern = str_repeat($pattern, 5);
    
    echo $repeatedPattern; // Outputs: *****
    ?>
    
  3. Dynamic string repetition in PHP:

    You can make the repetition dynamic by using variables. For instance:

    <?php
    $text = "abc";
    $repeatCount = 4;
    $result = str_repeat($text, $repeatCount);
    
    echo $result; // Outputs: abcabcabcabc
    ?>
    
  4. Generate repeated patterns with str_repeat() in PHP:

    You can generate patterns by repeating specific strings. Here's an example:

    <?php
    $pattern = "123";
    $repeatedPattern = str_repeat($pattern, 3);
    
    echo $repeatedPattern; // Outputs: 123123123
    ?>
    
  5. PHP str_repeat() for formatting output:

    str_repeat() is often used for formatting output, especially in cases where you want to create a repeated structure. For example:

    <?php
    $title = "Welcome";
    $underline = str_repeat("-", strlen($title));
    
    echo $title . "\n";
    echo $underline . "\n";
    ?>
    

    Outputs:

    Welcome
    -------