在写这篇文章之前,我做了一些研究。这是我第一次用PHP编写代码。我确实使用了<php include 'vars.php'; >,但那只是谷歌的一个复制粘贴。
<?php
echo "<table>";
for ($x = 1; $x <= 10; $x++) {
${'game' . $x} = $x;
$game1 = "GTA5";
$game2 = "Dirt3";
$game3 = "Skyrim";
echo "<th>".$game."</th>";
}
echo "</table>";
?>我想做一个循环,其中变量在每个循环中改变,并输出一个预设的名称或文本。我希望在这里发生的是,在每次循环之后,$x将输出"game1“、"game2”、"game3“等,因为我已经预先设置了变量。
$game1 = "GTA5";
$game2 = "Dirt3";
$game3 = "Skyrim";我原以为<th>内部会改为"GTA5" "Dirt3" "Skyrim"。
为什么我需要这个?我已经创建了一个表,这是我想要循环的部分。
<th><div class='text'>GTA5</div><div class='grid'><div onclick='()' class="res">1080p ></div><div onclick='()' class="res">1440p ></div><div onclick='()' class="res">4K ></div></div></th>
<th><div class="text"><p>GTA5</p><span>1080p</span></div><div onclick='()' class="btn">></div></th>
<th><div class="text"><p>GTA5</p><span>min FPS</span></div></th>
<th><div class="text"><p>GTA5</p><span>max FPS</span></div></th>
<th><div class="text"><p>GTA5</p><span>used Vram</span></div></th>
<th><div class="text"><p>GTA5</p><span>1440p</span></div><div onclick='()' class="btn">></div></th>
<th><div class="text"><p>GTA5</p><span>min FPS</span></div></th>
<th><div class="text"><p>GTA5</p><span>max FPS</span></div></th>
<th><div class="text"><p>GTA5</p><span>used Vram</span></div></th>
<th><div class="text"><p>GTA5</p><span>4k</span></div><div onclick='()' class="btn">></div></th>
<th><div class="text"><p>GTA5</p><span>min FPS</span></div></th>
<th><div class="text"><p>GTA5</p><span>max FPS</span></div></th>
<th><div class="text"><p>GTA5</p><span>used Vram</span></div></th>我需要将这个循环13次,在每次循环中,名称"GTA5"都需要更改。第一个小代码只是对更大的东西的一次“尝试”。
发布于 2017-08-21 07:57:21
不使用$game1、$game2、$game3等,而是将变量$game转换为一个包含游戏列表的数组。
<?php
$game = array("GAME1","GAME2","GAME3","ETC");
$screen = array("4K","1080p","720p","480p");
echo '<table>';
$i=0;
while($i <4){
echo '<th><div class="text"><p>'.$game[$i].'</p><span>'.$screen[$i].'</span></div><div onclick="()" class="btn"></div></th>';
echo '<th><div class="text"><p>'.$game[$i].'</p><span>min FPS</span></div></th>';
echo '<th><div class="text"><p>'.$game[$i].'</p><span>max FPS</span></div></th>';
$i++;
}
echo '<table>';https://stackoverflow.com/questions/45787656
复制相似问题