简体   繁体   中英

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

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.

Don't multiply strings. You can either 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);

使用str_repeat()

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

Use a for loop.....................

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM