str_repeat – PHP String Functions

Syntax :

str_repeat (string, repeat );

Description :

str_repeat() function repeats a string a specified number of times..


Parameter :

  • string – This is a Required parameter. It is the input string which will be repeated.
  • repeat – This is a Required parameter. It will set how many times a input string will be repeated. It should be greater than or equal to 0. If its value is set to 0, the function will return an empty string.

Output :

This will return a input string repeated number of times.


Related articles : str_pad(), str_word_count().


str_repeat() – PHP Functions Example 1 :
<?php
echo str_repeat("!",10);
?>

In above example ,We have a string “!”. Now this function will repeat it 10times. Output of above code in the browser is as below:

!!!!!!!!!!

str_repeat() – PHP Functions Example 2 : It will return empty string as repeat variable value is set to 0.
<?php
var_dump(str_repeat("!",0));
?>

In above example ,We have a string “!”. Now this function will return empty string because if repeat values is set to 0, the function will return an empty string.. Output of above code in the browser is as below:

string(0) “”

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *