您的位置 首页 php

「基础编程学习」 「PHP7数组详解」:第1章 (6)循环结构

# 1.11 循环结构

循环,这个太常用了。我们为什么使用计算机,而不是手动一个一个处理,就是因为计算机善于处理循环的结构。把最枯燥的部分,扔给机器,它喜欢这样。

循环的应用场景,很多。比如对于数组的处理,我们在程序当中,总是有很多规格的数据要处理,转化,转换,格式化。而数组,也即是我们本书所要讲的重点知识,在PHP中非常常见。

PHP中的循环结构,有3中语法结构。

  1. for循环
  2. foreach 循环
  3. while循环

1 – for循环

话不多说,直接上代码。看下面这段:

「基础编程学习」 「PHP7数组详解」:第1章 (6)循环结构

for语句内位置参数,第一个是循环因子的初始值,第二个是跳出循环的判断条件,第三个是运行式。

上述代码中,在条件判断else中使用continue语句,表示,这一轮算了,就这样了,不要管了,直接开始下一轮循环吧。默认跳出一层循环。如果循环层数多了,可以指定跳出几层。

下面我们举一个直观的例子,大家在视觉上对for循环有个认识。我们输出10个进度条,分别从10%-100%,递加10%。

为了说明效果,HTML和PHP混合写了,大家知道这个运行机制就可以了,这不是最佳实践。

注意样式是使用的 bs

老规矩,在浏览器内访问 www.array.com/chapter1.5.php ,刷新后效果如下图:

「基础编程学习」 「PHP7数组详解」:第1章 (6)循环结构

怎么样,效果是不是很直观。一共10个循环,每次递增10,展示出来就是这样了。

2 – foreach循环

foreach 语法结构提供了遍历数组的简单方式。

foreach 仅能够应用于数组和对象 ,如果尝试应用于其他数据类型的变量,或者未初始化的变量将发出错误信息。

你如果曾留意一些框架,或者代码库,对此君一定不会陌生。没错儿,到处都是它。

先列举几个简单的列子,大家看一看,应该就能明白语法结构了。

「基础编程学习」 「PHP7数组详解」:第1章 (6)循环结构

$temp是一个数组。再看复杂一些的:

「基础编程学习」 「PHP7数组详解」:第1章 (6)循环结构

这次循环的时候使用 $k=>$v结构,$k保留索引值,$v保留数值。

PHP内的foreach循环大抵如此。我们使用时,大可不必考虑数组长度,只用考虑数组是一个什么结构,是关联数组,还是一维简单数组,对应处理即可。

3 – while循环

while循环用的也应该算是最低的,为什么?一不留神,死循环!会一直到PHP的timeout时间30秒。这是不允许出现的。

还是通过代码认识一下其结构。

「基础编程学习」 「PHP7数组详解」:第1章 (6)循环结构

上面这段代码,是对文件的操作 句柄 $ fp ,判断其是否到了文件结尾feof()函数。

如果不是文件末尾,继续循环。执行结构体内的语句。

当然,上面代码中使用if判断语句,如果执行到一行,没有任何字符,仅仅是换行符,这个说明是空行,那也直接执行 break 。这也能 跳出循环

有时候为了写一个命令行运行的文件,要守护进程,永远不过期,永远不退出,那可能就需要一个死循环,用云运行下去。下面的代码看一下:

「基础编程学习」 「PHP7数组详解」:第1章 (6)循环结构

大家看,这就是一个while(true)典型的死循环。为什么这么用?因为我们不知道到底要处理多少数据,不知道到底有多少循环。因此只能这样,硬着头皮走下去,那么在遇到合适的跳出条件时,使用break跳出循环。

在订单处理中,预先不能预估数据量的话,不用这样的办法,可用的选择似乎并不多。

为了加深印象,我们再看一个完整的函数:

「基础编程学习」 「PHP7数组详解」:第1章 (6)循环结构

上述函数解析url内的query string部分,类似 a=1&b=3&c=9这样,一个一个地截断操作。知道将 字符串 字段到最后没有任何值,那么 strlen ($nvpstr) === 0,这时候while循环退出,函数返回。

大家完全可以发挥想象力,使用这简单的结构,构造出复杂的应用。

文章来源:智云一二三科技

文章标题:「基础编程学习」 「PHP7数组详解」:第1章 (6)循环结构

文章地址:https://www.zhihuclub.com/76622.shtml

关于作者: 智云科技

热门文章

评论已关闭

36条评论

  1. Hims sildenafil is the most popular and the most affordable option for ED treatment In all these cases, the drugs will be a guarantee of solid erection

  2. We want to major on the majors and minor on the minors for the sake of unity in the bride of Christ

  3. If you are buying SARMs, peptides, or any other bodybuilding supplements, then you have probably heard of Science. PubMed CrossRef Google Scholar.

  4. Countless people suffer decades later from chronic Lyme. It was a pretty emotional roller coaster with all the appointments, etc.

  5. TTh has beneficial effects on muscle mass and strength that may reduce the propensity to fall and therefore decrease fracture risk

  6. August 20, 2005 NewsWithViews Transfection with small interfering si RNA for SFRP5 knockdown and LPS treatment in 3 T3 L1 cells

  7. But Busch said the car got inside of his wake and got too close to him and it pulled me right into him

  8. benazepril clarinex syrup discontinued Don t be surprised if we invest in projects that seem strange or speculative compared with our existing Internet businesses, he wrote on his Google profile

  9. 1, Infectious thyroiditis, also referred to as either acute or chronic, and which in fact may be either, along with the qualifying term suppurative AST, nonsuppurative, or septic thyroiditis One nanogram of cDNA was suspended in Tagment DNA Buffer, and tagmentation fragmentation and tagging with the adaptors was performed with the Nextera enzyme Amplicon Tagment Mix, FC 131 1096, incubating at 55 C for 10 minutes

  10. It is associated with multiple symptoms in both patients and caregivers, such as stress, anxiety, depression, sleep disturbance, and fatigue

  11. Monitor Closely 1 rucaparib will increase the level or effect of gefitinib by affecting hepatic intestinal enzyme CYP3A4 metabolism In this case, hormone receptor status gives information about your breast cancer

  12. Our next step will be to provide our surgeons with recommendations regarding the number of doses that will satisfy the pain needs of the majority of patients

  13. Ma mГЎr magas szintГ» evidenciГЎk alapjГЎn kerГјlhet alkalmazГЎsra a palbociclib Г©s letrozol kombinГЎciГіja a hormonГ©rzГ©keny emlГµrГЎkok kezelГ©sГ©ben, vagy a palbociclib fulvesztranttal egyГјtt adva endokrin terГЎpiГЎra rezisztens tumorok esetГ©ben

  14. and the Reproductive Endocrinology and Infertility fellowship at UT Southwestern Medical Center

  15. Many of these developments have reinjected produced water into the hydrocarbon formations, and I have never seen any reports of water table contamination We compared the cumulative rates of ovulation as well as other rates and proportions between groups using relative rates, rates differences, and П‡ 2 test statistics using SPSS 11

  16. Anadrol Powder per 1 gram of Oxymetholone Produces Highest concentration made 50 mg ml 3 K genes were identified as significant at an adjusted P value of 0

  17. Of the three higher quality studies in depressed women, two 132, 133 showed antidepressant efficacy of ERT transdermal patches compared with placebo in perimenopausal women N 34 and N 50

  18. Diagnostic advice about our patients from you a normal post- iui com Cruz, 42, who released a copy of his birth certificate on Monday, was born in Canada s oil capital Calgary, where his Cuban born father was working in the energy industry

  19. Using the final model of covariates, associations with post AKI mean fluid balance and furosemide dose were evaluated in the four predefined subgroups Table 4

  20. Atypical endometrial hyperplasia AH is considered a high risk precursor to EC and hysterectomy is typically recommended Therefore, we hypothesized that the inhibition of microglial activation by tamoxifen may be helpful in delaying photoreceptor degeneration in RP

  21. Technetium Tc 99m ethyl cysteinate dimer brain single photon emission CT in mild traumatic brain injury a prospective study

  22. Do not drive or participate in hazardous activities for 24 hours after your first dose, any increase in your dosage, or restarting treatment The safety of physical activity in breast cancer survivors, even during treatment, has been documented in numerous trials

  23. In addition, there are differences between regions in the sex distribution; for example, rheumatic heart disease commonly affects women more than men 31, 32, and peripartum cardiomyopathy is particularly common in Africa 33

  24. swelling, flushing warmth, redness, or tingly feeling, headache, dizziness, nausea, and feeling weak or tired Heywood, Loma Linda University Medical Center, Loma Linda, Calif

  25. FatГ©ma Dodat does not work for, consult, own shares in or receive funding from any company or organisation that would benefit from this article, and has disclosed no relevant affiliations beyond their academic appointment himcolin gel taking clarithromycin with prednisolone It must still be agreed by the rights holder, governing body and teams 11 at present

  26. Another thing if on Clomid will you ovulate as stated by doctor those perticular days

网站地图