• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • VCS
  • Interview Questions
  • Database
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

PHP function str_repeat() – Repeat a string

by admin

The str_repeat() function is used to repeat a string, a specified number of times. This function was introduced in PHP4. The function returns a string consisting of count copies of string appended to each other. If the count is not greater than 0, an empty string is returned.

Syntax:

str_repeat(string $string, int $times): string

Parameters

Parameter Description
string The string to be repeated.
times Number of time the string string should be repeated.
Note: times has to be greater than or equal to 0. If the times is set to 0, the function will return an empty string.

Return Values

Returns the repeated string.

Examples

Example 1:

<?php
echo str_repeat(“*-”, 8);
?>

The output of the above program is as follows:

*-*-*-*-*-*-*-*-

Example 2: Print a small Dots-and-Boxes game grid.

<?php 
echo str_repeat (str_repeat (' .', 10) . "\n", 10); 
?>

Output:

. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . .

Filed Under: PHP

Some more articles you might also be interested in …

  1. PHP function chunk_split() – Split a string into smaller chunks
  2. PHP function acos – Arc cosine
  3. PHP function abs – Absolute value
  4. How to test a PHP script
  5. PHP ceil function – Round fractions up
  6. PHP Function strncasecmp() – Binary safe case-insensitive string comparison of the first n characters
  7. PHP cos function – Cosine
  8. PHP base_convert function – Convert a number between arbitrary bases
  9. PHP function convert_cyr_string() – Convert from one Cyrillic character set to another
  10. PHP Function str_split() – Convert a string to an array

You May Also Like

Primary Sidebar

Recent Posts

  • Vanilla OS 2 Released: A New Era for Linux Enthusiasts
  • mk Command Examples
  • mixxx Command Examples
  • mix Command Examples

© 2025 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright