David19801
David19801

Reputation: 11448

make string of N characters

Say I have a number=20 and a $value=7.

If I want to make a string of 20 7's using PHP. what's the quickest way to make this?

So output like:

$a='77777777777777777777';

Any functions to make this easier?

Upvotes: 30

Views: 13677

Answers (5)

Joachim Isaksson
Joachim Isaksson

Reputation: 180927

str_repeat is pretty easy

$a = str_repeat('7', 20);

Upvotes: 6

Adam Zalcman
Adam Zalcman

Reputation: 27233

You can use str_repeat(), e.g.

$a=str_repeat('7', 20);

Upvotes: 12

DaveRandom
DaveRandom

Reputation: 88667

$a = str_repeat('7', 20);

str_repeat()

...or maybe:

$a = str_pad('', 20, '7');

str_pad()

Upvotes: 4

Freyja
Freyja

Reputation: 40814

Use str_repeat():

$a = str_repeat('7', 20);

Upvotes: 7

Eugen Rieck
Eugen Rieck

Reputation: 65284

$a=str_repeat($value,$number);

Upvotes: 54

Related Questions