How to repeat a string to a specific number of times in PHP ?
Last Updated : 18 Jul, 2024
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>"); ?>
OutputOriginal 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>"); ?>
OutputOriginal 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"); ?>
OutputOriginal 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"; ?>
OutputOriginal string : Hi!GFG User. Final string : Hi!GFG User.Hi!GFG User.Hi!GFG User.
Similar Reads
How to get the position of character in a string in PHP ? In this article, we will get the position of the character in the given string in PHP. String is a set of characters. We will get the position of the character in a string by using strpos() function. Syntax: strpos(string, character, start_pos) Parameters: string (mandatory): This parameter refers t
2 min read
How to replace multiple characters in a string in PHP ? A string is a sequence of characters enclosed within single or double quotes. A string can also be looped through and modifications can be made to replace a particular sequence of characters in it. In this article, we will see how to replace multiple characters in a string in PHP.Using the str_repla
3 min read
How to remove the first character of string in PHP? Remove the very first character of a given string in PHP Examples: Input : GeeksforgeeksOutput : eeksforgeeksInput :, Hello geek!Output : Hello geek!Explanation:In PHP to remove characters from the beginning we can use ltrim but in that, we have to define what we want to remove from a string i.e. re
3 min read
How to replace String in PHP ? Replacing a string in PHP involves substituting parts of a string with another string. Common methods include str_replace() for simple replacements, preg_replace() for pattern-based replacements, substr_replace() for positional replacements, and str_ireplace() for case-insensitive replacements. Each
2 min read
How to read each character of a string in PHP ? A string is a sequence of characters. It may contain integers or even special symbols. Every character in a string is stored at a unique position represented by a unique index value. Here are some approaches to read each character of a string in PHPTable of ContentUsing str_split() method - The str_
4 min read
Insert string at specified position in PHP Given a sentence, a string and the position, the task is to insert the given string at the specified position. We will start counting the position from zero. See the examples below. Input : sentence = 'I am happy today.' string = 'very' position = 4 Output :I am very happy today.Input : sentence = '
3 min read