简体   繁体   English

多次回显PHP变量

[英]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() . 在这个简单的例子中,您可以使用str_repeat()

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

Reference: PHP string functions 参考: PHP字符串函数

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循环中:

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

Or str_repeat: 或者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..................... 使用for循环.....................

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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