str_repeat() Function in PHP 8.1 and 8.2 With Example

Support PHP Version: PHP 7.1, PHP 7.2, PHP 7.3, PHP 7.4, PHP 8.0, PHP 8.1, PHP 8.2, PHP 8.3 With Latest All Version Support.

Hello Friends Today, through this tutorial, I will tell you How do you use `str_repeat()` function using PHP, PHP 8, PHP 8.1, PHP 8.2, PHP 8.3 With Example. The `str_repeat()` function in PHP is used to repeat a string a specified number of times. This function is available in both PHP 8.1 and PHP 8.2.

Here’s how you can use it with an example:

<?php
// Repeat the string "Hello" three times
$repeated_string = str_repeat("Hello", 3);
// Output the repeated string
echo $repeated_string;
?>

In this example, the `str_repeat()` function is called with two parameters:

1. The string to repeat (`”Hello”`).
2. The number of times to repeat the string (`3`).

The function concatenates the input string the specified number of times and returns the result.

The output will be:

HelloHelloHello

You can use `str_repeat()` for various purposes such as generating placeholder strings, formatting output, or building repeated patterns.