• 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. “Access denied for user ‘username’@’hostname’ (using password: YES)” – Error while connecting MySQL with PHP
  2. PHP base_convert function – Convert a number between arbitrary bases
  3. PHP function strcmp() – Binary safe string comparison
  4. PHP function acos – Arc cosine
  5. htaccess Cheatsheet
  6. PHP Function strncasecmp() – Binary safe case-insensitive string comparison of the first n characters
  7. PHP function abs – Absolute value
  8. PHP function strlen() – Get string length
  9. PHP Function str_split() – Convert a string to an array
  10. How to test a PHP script

You May Also Like

Primary Sidebar

Recent Posts

  • fprintd-delete Command Examples in Linux
  • fprintd-delete: command not found
  • foreman: command not found
  • foreman Command Examples in Linux

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright