Open In App

How to repeat a string to a specific number of times in PHP ?

Last Updated : 18 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

A string is a sequence of characters stored in PHP. The string may contain special characters or numerical values or characters. The strings may contain any number of characters and may be formed by the combination of smaller substrings. 

Using for loop

An empty string variable is declared in order to store the contents of the final string. An integer value n is declared that indicates the number of times to repeat the string. Each time the original string is appended to the end of the final string variable declared. The contents of the final string are then displayed. 

PHP
<?php

// Declaring string variable 
$str = "Hi!GFG User.";
echo("Original string : ");
echo($str . "</br>");

// Declaring number of times 
$n = 3;

// Declaring empty string
$final_str = "";

// Looping over n times
for($i = 0; $i < $n; $i++) {

      // Appending str to final string value
    $final_str .= $str;
}
echo("Final string : ");
echo($final_str . "</br>");

?>

Output
Original string : Hi!GFG User.
Final string : Hi!GFG User.Hi!GFG User.Hi!GFG User.

Using str_repeat method

The in-built str_repeat() method in PHP can be used to repeat the specified string the specified number of times. The contents can be stored in a separate variable name. The method has the following syntax : 

str_repeat( str, n)

Parameters: 

  • str – It is the original string.
  • n – The number of times to repeat a string.
PHP
<?php
  
// Declaring string variable 
$str = "Hi!GFG User.";
echo("Original string : ");
echo($str."</br>");

// Declaring number of times 
$n = 3;

// Computing final string
$final_str = str_repeat($str, $n);
echo("Final string : ");
echo($final_str . "</br>");

?>

Output
Original string : Hi!GFG User.
Final string : Hi!GFG User.Hi!GFG User.Hi!GFG User.

Using Recursion

In this approach, we create a recursive function that concatenates the original string with itself until the specified number of repetitions is achieved.

Example: In this example, we will use the Using Recursion.

PHP
<?php 

// Function to repeat string using recursion
function repeatString($str, $n) {
    // Base case: if n is 0, return an empty string
    if ($n == 0) {
        return "";
    }
    // Recursive case: concatenate the string with the result of the function called with n-1
    return $str . repeatString($str, $n - 1);
}

// Declaring string variable
$str = "Hi!GFG User.\n";
echo("Original string : ");
echo($str . "\n");

// Declaring number of times
$n = 3;

// Computing final string using recursion
$final_str = repeatString($str, $n);
echo("Final string : ");
echo($final_str . "\n");

?>

Output
Original string : Hi!GFG User.

Final string : Hi!GFG User.
Hi!GFG User.
Hi!GFG User.


Using array_fill and implode

Use array_fill to create an array filled with the original string repeated n times, then concatenate the array elements using implode. This approach efficiently repeats a string multiple times in PHP.

Example:

PHP
<?php

// Declaring string variable 
$str = "Hi!GFG User.";
echo "Original string : " . $str . "\n";

// Declaring number of times 
$n = 3;

// Using array_fill to create an array with $str repeated $n times
$array = array_fill(0, $n, $str);

// Using implode to concatenate the array elements into a single string
$final_str = implode('', $array);

echo "Final string : " . $final_str . "\n";

?>

Output
Original string : Hi!GFG User.
Final string : Hi!GFG User.Hi!GFG User.Hi!GFG User.


Next Article

Similar Reads