简体   繁体   English

有没有更好的方法将许多值分配给变量并在PHP中按顺序回显它们?

[英]Is there a better way to assign many values to variables and echo them in order in PHP?

OK, at the top of my page I have a bunch of php variables like this: OK,在我页面的顶部,我有一堆这样的php变量:

<?php 
$description_1 = 'This and that';
$description_2 = 'This and this and that';
?>

And this data is then pulled into HTML markup using the echo syntax, but the thing is that I have created hundreds of similar code to allow for the above variables to be echoed, in other words, image the below: 然后使用echo语法将此数据提取到HTML标记中,但事实是,我已经创建了数百个相似的代码,以允许上述变量被生成,换句话说,对下面的图像进行成像:

<p><?php echo $description_1; ?></p>

and then repeated 200 times: 然后重复200次:

<p><?php echo $description_2; ?></p>

Is there a more efficient way of doing this? 有更有效的方法吗? I am sure there is.... 我敢肯定有...

Thanks! 谢谢!

As stated by @chris85 , use arrays: @ chris85所述 ,请使用数组:

$description = [
  'This and that',
  'This and this and that'
];

foreach($description as $value){
  echo "<p>$value</p>";
}

The first index is 0 , incrementing after each assignment so: 第一个索引为0 ,在每次分配后递增,因此:

echo $description[1]; // This and this and that
$description = array('This and that', 'This and that and that');
foreach($description as $row){
  echo '<p>'.$row.'</p>'
}

or 要么

$description = 'This and that';
$description .= 'This and that and that';

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

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