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.


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

str_repeat($str, $N)


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

  $str = 'Apple';
  $N = 5;
  $output = str_repeat($str, $N);
  echo $output;


PHP - Repeat string for N times


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.