In this PHP tutorial, you shall learn how to repeat a given string for N times using str_repeat() function, with example programs.

PHP – Repeat string for N times

To repeat a string for N times in PHP, call str_repeat() function and pass the given string and integer N as arguments. The function returns a new string created with the original repeated N times, or original string appended N times.

Syntax

The syntax to repeat a string str for N number of times is

</>
Copy
str_repeat($str, $N)

Examples

1. Repeat string for 5 times

In this example, we take a string in str and a number in N. We shall create a new string with the string str repeated for N times.

PHP Program

</>
Copy
<?php
  $str = 'Apple';
  $N = 5;
  $output = str_repeat($str, $N);
  echo $output;
?>

Output

PHP - Repeat string for N times

Conclusion

In this PHP Tutorial, we learned how to repeat a given string for N number of times, using str_repeat() function, with the help of examples.