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

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • 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. How to test a PHP script
  2. PHP function stripslashes() – Un-quotes a quoted string
  3. PHP Function str_word_count() – Return information about words used in a string
  4. PHP bindec function – Convert a number between arbitrary bases
  5. htaccess Cheatsheet
  6. PHP base_convert function – Convert a number between arbitrary bases
  7. PHP function str_replace() – Replace all occurrences of the search string with the replacement string
  8. PHP function abs – Absolute value
  9. PHP ceil function – Round fractions up
  10. PHP atan function – Arc tangent

You May Also Like

Primary Sidebar

Recent Posts

  • JavaFX ComboBox: Set a value to the combo box
  • Nginx load balancing
  • nginx 504 gateway time-out
  • Images preview with ngx_http_image_filter_module

© 2022 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright