Repeat multiple PHP variable

How could a repeat of a variable be repeated several times.

To better understand this issue, it would be if I said:

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

This is probably the easiest thing, but I'm not sure.

AND

+4
source share
5 answers

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

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

Link: PHP String Functions

For something more complex, a loop is usually a transition method.

+16
source

Do not propagate lines. You can do it manually:

 echo $variable; echo $variable; echo $variable; echo $variable; // etc 

Or in a for loop:

 for($z=0;$z<10;$z++){ echo $variable; } 

Or str_repeat:

 echo str_repeat($variable, 10); 
+4
source

Use str_repeat() .

 echo str_repeat($foo, 7); 
+2
source
 for ($i = 0, $i <= 7, $i++) { echo $foo; } 
+1
source

Use the for loop .....................

http://php.net/manual/en/control-structures.for.php

0
source

Source: https://habr.com/ru/post/1336728/


All Articles