Blogger Information
Blog 4
fans 0
comment 0
visits 3193
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP的for、foreach与JavaScript的for、forEach的区别
Keroro丶前行
Original
666 people have browsed it

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>
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post