PHP
区别:
PHP中变量直接使用
$
符号,JS 中使用let
或const
。count()
为PHP系统函数 与 JS 中的length
一样是获取数组的长度。
PHP中连接符使用实心圆点.
JS 中使用加号+
。
1.【php】for &【JS】for
//PHP&for
<?
$nums = ["天河区","白云区","越秀区","荔湾区","海珠区","黄埔区","萝岗区","番禺区","增城区","从化区"];
for ($i=0; $i < count($nums) ; $i++) {
echo $nums[$i].'<br/>';
}
?>
//JavaScript&for
<script>
let nums = ["天河区","白云区","越秀区","荔湾区","海珠区","黄埔区","萝岗区","番禺区","增城区","从化区"];
for (let i=0; i < nums.length ; i++) {
console.log('广州市' + nums[i]);
}
</script>
1.【php】foreach &【JS】forEach
模板语法
{
可以省略用:
代替 ,echo
可以省略不写用=
代替,}
用endforeach
代替以;
结束
//PHP&foreach
<?
$nums = ["天河区","白云区","越秀区","荔湾区","海珠区","黄埔区","萝岗区","番禺区","增城区","从化区"];
//foreach (循环变量 as 当前子元素)
foreach ($nums as $num) :
= $num.'<br/>';
endforeach;
?>
//JavaScript&for
<script>
let nums = ["天河区","白云区","越秀区","荔湾区","海珠区","黄埔区","萝岗区","番禺区","增城区","从化区"];
nums.forEach(num => console.log(num))
</script>