博客列表 >PHP的for、foreach与JavaScript的for、forEach的区别

PHP的for、foreach与JavaScript的for、forEach的区别

Keroro丶前行
Keroro丶前行原创
2021年04月27日 17:57:29674浏览

PHP

区别:

PHP中变量直接使用$符号,JS 中使用letconst
count()为PHP系统函数 与 JS 中的 length一样是获取数组的长度。
PHP中连接符使用实心圆点.JS 中使用加号+

1.【php】for &【JS】for

  1. //PHP&for
  2. <?
  3. $nums = ["天河区","白云区","越秀区","荔湾区","海珠区","黄埔区","萝岗区","番禺区","增城区","从化区"];
  4. for ($i=0; $i < count($nums) ; $i++) {
  5. echo $nums[$i].'<br/>';
  6. }
  7. ?>
  1. //JavaScript&for
  2. <script>
  3. let nums = ["天河区","白云区","越秀区","荔湾区","海珠区","黄埔区","萝岗区","番禺区","增城区","从化区"];
  4. for (let i=0; i < nums.length ; i++) {
  5. console.log('广州市' + nums[i]);
  6. }
  7. </script>

1.【php】foreach &【JS】forEach

模板语法{可以省略用:代替 ,echo可以省略不写用=代替, }endforeach代替以;结束

  1. //PHP&foreach
  2. <?
  3. $nums = ["天河区","白云区","越秀区","荔湾区","海珠区","黄埔区","萝岗区","番禺区","增城区","从化区"];
  4. //foreach (循环变量 as 当前子元素)
  5. foreach ($nums as $num) :
  6. = $num.'<br/>';
  7. endforeach;
  8. ?>
  1. //JavaScript&for
  2. <script>
  3. let nums = ["天河区","白云区","越秀区","荔湾区","海珠区","黄埔区","萝岗区","番禺区","增城区","从化区"];
  4. nums.forEach(num => console.log(num))
  5. </script>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议