• 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. PHP function abs – Absolute value
  2. PHP decbin function – Decimal to binary
  3. PHP function str_replace() – Replace all occurrences of the search string with the replacement string
  4. PHP Function str_word_count() – Return information about words used in a string
  5. PHP function strcasecmp() – Binary safe case-insensitive string comparison
  6. PHP function chop() – Alias of rtrim()
  7. PHP function stripcslashes() – Un-quote string quoted with addcslashes()
  8. PHP function stripslashes() – Un-quotes a quoted string
  9. “Access denied for user ‘username’@’hostname’ (using password: YES)” – Error while connecting MySQL with PHP
  10. PHP function convert_cyr_string() – Convert from one Cyrillic character set to another

You May Also Like

Primary Sidebar

Recent Posts

  • base64 Command Examples (Encode or decode file or standard input to/from Base64, to standard output)
  • base32 Command Examples (Encode or decode file or standard input to/from Base32, to standard output)
  • banner Command Examples (Print the given argument as a large ASCII art)
  • bandwhich Command Examples (Display the current network utilization by process, connection or remote IP/hostname)

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright