您的位置 首页 php

PHP程序员从入门到佛系第十七弹:PHP 循环-For 循环

PHP程序员从入门到佛系第十七弹:PHP 循环-For 循环

PHP 循环 – For 循环


循环执行代码块指定的次数,或者当指定的条件为真时循环执行代码块。


for 循环

for 循环用于您预先知道脚本需要运行的次数的情况。

语法

for (初始值; 条件; 增量)
{
 要执行的代码;
}
 

参数:

  • 初始值 :主要是初始化一个变量值,用于设置一个计数器(但可以是任何在循环的开始被执行一次的代码)。
  • 条件 :循环执行的限制条件。如果为 TRUE,则循环继续。如果为 FALSE,则循环结束。
  • 增量 :主要用于递增计数器(但可以是任何在循环的结束被执行的代码)。

注释: 上面的 初始值 增量 参数可为空,或者有多个表达式(用逗号分隔)。

实例

下面的实例定义一个初始值为 i=1 的循环。只要变量 i 小于或者等于 5,循环将继续运行。循环每运行一次,变量 i 就会递增 1:

PHP程序员从入门到佛系第十七弹:PHP 循环-For 循环

<?php
for ($i=1; $i<=5; $i++)
{
 echo "The number is " . $i . "<br>";
}
?>
 

输出:

The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
 

foreach 循环

foreach 循环用于遍历数组。

语法

foreach ($array as $value)
{
 要执行代码;
}
 

每进行一次循环,当前数组元素的值就会被赋值给 $value 变量( 数组指针 会逐一地移动),在进行下一次循环时,您将看到数组中的下一个值。

实例

下面的实例演示了一个输出给定数组的值的循环:

PHP程序员从入门到佛系第十七弹:PHP 循环-For 循环

<?php
$x=array("one","two","three");
foreach ($x as $value)
{
 echo $value . "<br>";
}
?>
 

输出:

one
two
three
 
PHP程序员从入门到佛系第十七弹:PHP 循环-For 循环

笔记

使用 for循环 ,实现 冒泡排序

<?php
$arr = array(5,3,6,2,8,10);
for($i = count($arr)-1;$i>=0;$i--){
 for($j = 0 ; $j < $i ; $j++){
 if($arr[$j+1] > $arr[$j] ){
 $aa = $arr[$j+1];
 $arr[$j+1] = $arr[$j];
 $arr[$j] = $aa;
 }
 }
}
print_r($arr);
?>
 
PHP程序员从入门到佛系第十七弹:PHP 循环-For 循环

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

文章标题:PHP程序员从入门到佛系第十七弹:PHP 循环-For 循环

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

关于作者: 智云科技

热门文章

评论已关闭

29条评论

  1. Brains even small ones are dauntingly complex information flows in parallel through many different circuits at once; different components of a single functional circuit may be distributed across many brain structures and be spatially intermixed with the components of other circuits; feedback signals from higher levels constantly modulate the activity within any given circuit; and neuromodulatory chemicals can rapidly alter the effective wiring of any circuit celecoxib, trandolapril

  2. The survey took me less than 2 minutes to fill out and they said they will pay 5 cash for surveys received by the second of November

  3. The flow rate was set at 1 mL min with excitation wavelength at 295 nm and emission wavelength at 325 nm In many cases, Wellbutrin will be discontinued

  4. Parvalbumin interneurons modulate striatal output and enhance performance during associative learning Phenyl exMSN PEG TA Synthesis With Different TEOS Phenyl silane Ratio

  5. Therefore, we performed identical experiments in the presence of the transcription inhibitor actinomycin D

  6. com 20 E2 AD 90 20Viagra 20Kaufen 20In 20Der 20Schweiz 20 20Viagra 20Femenino 20En 20Gotas 20En 20Argentina viagra femenino en gotas en argentina I knew one guy once that was literally told that since their couldn t take more out of his check due to federal guidelines, they will take out the maximum that they can, and put even more into arrears so he can continue paying even into his old age, with interest

  7. Warfarin An interaction study with warfarin showed no clinically significant effect of FГ©mara on warfarin pharmacokinetics

  8. carbinoxamine and pentobarbital both increase sedation Dunn School of Pharmacy integrates new method for delivering video instruction

  9. Management of bone metastases may include a bisphosphonate drug Breastfeed Med 2011; 6 Suppl 1 S- 21

  10. nortriptyline and posaconazole both increase QTc interval Inhibition of angiogenesis by blockers of volume- regulated anion channels

  11. However, federal funding has stalled, and just 5 of cancer patients participate in clinical trials

  12. I finally stopped taking that drug All preliminary testing, including pupils, extraocular motilities and confrontation fields, were unremarkable

  13. Monitor Closely 2 atorvastatin increases levels of aliskiren by unspecified interaction mechanism

  14. Symptoms of overdose are according to medication delivery type immediate release pills or extended release pills

  15. Following a single oral dose of 1, 000 mg metformin HCl extended release tablets two 500 mg tablets after a meal, the time to reach maximum plasma metformin concentration Tmax is achieved at approximately 7 8 hours

  16. I’m impressed, I must say. Rarely do I come across a blog that’s both equally educative and entertaining, and without a doubt, you’ve hit the
    nail on the head. The issue is something that too few people are speaking intelligently about.
    I am very happy I stumbled across this in my search for something regarding this.

网站地图