来源:互联网 更新时间:2023-07-15 13:45
今天号令小编为您带来php for循环有2种写法,分别是:1、“for(expr1; expr2; expr3){...}”方式;2、“for(expr1; expr2; expr3){if(...){}break;}...”方式。希望对大家有所帮助。
For循环是最近的循环语句之一,无论哪种语言,都有这个循环语句,也是我们工作中常用的循环方法。
语法规则:
for (expr1; expr2; expr3){
要执行的代码
}
expr1:表示循环开始的地方
expr2 :循环的条件,如果值为 TRUE,则继续循环,执行嵌套的循环语句。如果值为 FALSE,则终止循环。
expr3 :在每次循环之后被求值(并执行)。
写的有点晦涩难懂,我们写个最简单的for 循环demo吧!
<?php
for($n=1;$n<20;$n++){
echo 'for循环语句执行第'.$n."次<br>";
}
执行结果:
for循环语句执行第1次
for循环语句执行第2次
for循环语句执行第3次
for循环语句执行第4次
for循环语句执行第5次
for循环语句执行第6次
for循环语句执行第7次
for循环语句执行第8次
for循环语句执行第9次
for循环语句执行第10次
for循环语句执行第11次
for循环语句执行第12次
for循环语句执行第13次
for循环语句执行第14次
for循环语句执行第15次
for循环语句执行第16次
for循环语句执行第17次
for循环语句执行第18次
for循环语句执行第19次
可以看出,当$n<20的条件不满足的时候,就不输出$n。
<?php
for($n=1;$n<20;$n++){
if($n==10){
break;
}
echo 'for循环语句执行第'.$n."次<br>";
}
输出结果:
for循环语句执行第1次
for循环语句执行第2次
for循环语句执行第3次
for循环语句执行第4次
for循环语句执行第5次
for循环语句执行第6次
for循环语句执行第7次
for循环语句执行第8次
for循环语句执行第9次
当n等于10的时候,跳出循环,不继续执行。如果我们只是要跳出10,其他的继续执行,可以这样写:
<?php
for($n=1;$n<20;$n++){
if($n==10){
continue;
}
echo 'for循环语句执行第'.$n."次<br>";
}
这样我们就只是跳出一个循环,结果如下:
for循环语句执行第1次
for循环语句执行第2次
for循环语句执行第3次
for循环语句执行第4次
for循环语句执行第5次
for循环语句执行第6次
for循环语句执行第7次
for循环语句执行第8次
for循环语句执行第9次
for循环语句执行第11次
for循环语句执行第12次
for循环语句执行第13次
for循环语句执行第14次
for循环语句执行第15次
for循环语句执行第16次
for循环语句执行第17次
for循环语句执行第18次
for循环语句执行第19次
2025迷你世界雨薇免费激活码
2025崩坏星穹铁道7月3日兑换码
触屏时代的逆行者!劳斯莱斯坚持实体按键的真相曝光
光遇6.16每日任务怎么做
光遇6.19大蜡烛在哪里
《怪物乐土》哥布林猎手光暗选择技能加点
小米在全球范围推送澎湃OS 2.2 这几款机型现可升级
iPhone15pro怎么拍动态照片?
Perplexity AI比Google好吗 与传统搜索引擎对比
ChatGPT如何生成产品原型 ChatGPT产品设计辅助功能
如何轻松在iPhone上安装DeepSeek
悟空浏览器网页版:免费畅游网络世界的极速入口
原神梦见月瑞希圣遗物怎么搭配 原神梦见月瑞希圣遗物推荐
去年大肆宣传的AI功能没有兑现:苹果今年不敢画饼了
印度交通部长:有美国人抱怨印度道路设施比美国好
无限暖暖第四期巅峰赛速通攻略
燕云十六声燕云山寨寨主捏脸码
今天蚂蚁庄园答案最新6月13日
燕云十六声方旭怎么攻略
《奇迹世界:起源》暗黑套装获取攻略
手机号码测吉凶
本站所有软件,都由网友上传,如有侵犯你的版权,请发邮件haolingcc@hotmail.com 联系删除。 版权所有 Copyright@2012-2013 haoling.cc