PHP代码
<ul>
<?php
$cars = ['宝马x1','宝马x2','宝马x3','宝马x4'];
foreach($cars as $car){?>
<li><? echo $car?></li>
<?}?>
</ul>
JavaScript代码
<ul>
<script>
var cars = ['宝马x1','宝马x2','宝马x3','宝马x4'];
for (let i = 0; i < cars.length; i++) {
document.write("<li>"+cars[i]+"</li>");
}
</script>
</ul>
两者区别在于运行环境不同。PHP在服务端执行,JS在客户端执行。