Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Echo a PHP Variable multiple times

How would one go about echoing a variable multiple times..

A way to understand this question better would be if I said:

$foo = '<div>bar</div>';
echo $foo*7;

It's probably the most simple thing, but I'm not sure.

And

like image 471
Daryl Avatar asked Jan 24 '11 02:01

Daryl


People also ask

Can we store echo in variable PHP?

The variable $content will store the contents of both echo statements included above. What is the purpose of the variable variables in PHP? Ex: $$a= 'world'; Generally, double $ is used to append the variable value to a new variable to create that variable.

Does echo work in PHP?

The PHP echo StatementThe echo statement can be used with or without parentheses: echo or echo() .

What is $$ in PHP?

The $x (single dollar) is the normal variable with the name x that stores any value like string, integer, float, etc. The $$x (double dollar) is a reference variable that stores the value which can be accessed by using the $ symbol before the $x value. These are called variable variables in PHP.


1 Answers

In this simple case, you can use str_repeat().

$foo = '<div>bar</div>';
echo str_repeat($foo, 7);

Reference: PHP string functions

For anything more complex, a loop is usually the way to go.

like image 55
Pekka Avatar answered Nov 15 '22 02:11

Pekka