开发者

Echo a PHP Variable multiple times

开发者 https://www.devze.com 2023-02-06 21:00 出处:网络
How would one go about echoing a variable multiple times.. A way to understand this question better would be if I said:

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);


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


Use str_repeat().

echo str_repeat($foo, 7);


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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消