For loops in PHP

1. What‘s a Loop?

Programming can be tough work, and sometimes it‘s made tougher by having to do the same thing over and over. Let‘s say we want to echo a list of leap years in the editor. You might think we‘d have to type:

<?php
    echo 2004;
    echo 2008;
    echo 2012;
    // And so on
?>

But there‘s a better way!

loop is a useful bit of code that repeats a series of instructions for you. For instance, instead of typing echo many times like we did above, we can simply use the code in the editor to the right!

2.  ‘For loop Syntax‘

Cool, right? Let‘s go through the syntax slowly, step-by-step. Here‘s an example that just echos the numbers 0 to 9:

<?php
for ($i = 0; $i < 10; $i++) {
    echo $i;
}
// echoes 0123456789
?>

It breaks down like this:

  1. for loop starts with the for keyword. This tells PHP to get ready to loop!
  2. Next comes a set of parentheses (()). Inside the parentheses, we tell PHP three things, separated by semicolons (;): where to start the loop; where to end the loop; and what to do to get to the next iteration of the loop (for instance, count up by one).
  3. After the part in parentheses, the part in curly braces ({}) tells PHP what code to run for each iteration of the loop.

So the above example says: "Start looping with $i at 0, stop the loopbefore $i gets to 10, count up by 1 each time, and for each iteration, echo the current value of $i."

($i++ is shorthand for $i = $i + 1. You‘ll see this a lot!)

3. Loops + Arrays = ForEach

The foreach loop is used to iterate over each element of an object—which makes it perfect for use with arrays!

You can think of foreach as jumping from element to element in the array and running the code between {}s for each of those elements.

Let‘s walk through the foreach syntax step-by-step. First, here‘s a foreachloop that iterates over an array and prints out each element it finds:

<?php
  $numbers = array(1, 2, 3);

  foreach($numbers as $item) {
      echo $item;
  }
?>

First, we create our array using thearray() syntax we learned in the last lesson.

Next, we use the foreach keyword to start the loop, followed by parentheses. (This is very similar to what we‘ve done with for loops.)

Between the parentheses, we use the$numbers as $item) syntax to tell PHP: "For each thing in $numbers, assign that thing temporarily to the variable$item." (We don‘t have to use the name $item—just as with for loops, we can call our temporary variable anything we want.)

Finally, we put the code we want to execute between the curly braces. In this case, we just echo each element in turn.

4. Looping the Loop

A loop is a structure that tells a computer to execute a set of statements multiple times. If you have a process that you want repeated hundreds of times, it pays to put it in a loop so you don‘t need to write hundreds of lines of code.

If you are working on these courses in order, you have already seen how afor loop can allow for a set number of loop iterations. But what about a situation where (due to randomness, perhaps) you don‘t know how many times the loop should repeat? In that case, you can use a while loop.

while loop will execute as long as a certain condition is true. For example, the loop in the editor will simulate coin flips as long as the number of consecutive heads is less than 3.

时间: 2024-08-07 16:28:30

For loops in PHP的相关文章

USACO 4.1 Fence Loops(Floyd求最小环)

Fence Loops The fences that surround Farmer Brown's collection of pastures have gotten out of control. They are made up of straight segments from 1 through 200 feet long that join together only at their endpoints though sometimes more than two fences

Sort merge join、Nested loops、Hash join(三种连接类型)

目前为止,典型的连接类型有3种: Sort merge join(SMJ排序-合并连接): 首先生产driving table需要的数据,然后对这些数据按照连接操作关联列进行排序:然后生产probed table需要的数据,然后对这些数据按照与driving table对应的连接操作列进行排序:最后两边已经排序的行被放在一起执行合并操作.排序是一个费时.费资源的操作,特别对于大表.所以smj通常不是一个特别有效的连接方法,但是如果driving table和probed table都已经预先排序

洛谷P2738 [USACO4.1]篱笆回路Fence Loops

P2738 [USACO4.1]篱笆回路Fence Loops 11通过 21提交 题目提供者该用户不存在 标签USACO 难度提高+/省选- 提交  讨论  题解 最新讨论 暂时没有讨论 题目描述 农夫布朗的牧场上的篱笆已经失去控制了.它们分成了1~200英尺长的线段.只有在线段的端点处才能连接两个线段,有时给定的一个端点上会有两个以上的篱笆.结果篱笆形成了一张网分割了布朗的牧场.布朗想将牧场恢复原样,出于这个考虑,他首先得知道牧场上哪一块区域的周长最小. 布朗将他的每段篱笆从1到N进行了标号

[Javascript] Lodash: Refactoring Simple For Loops (_.find, _.findLast, _.filter)

This lesson shows how to refactor your old loops into using a simpler and more powerful lodash-style. We will start by looking at how many people traditionally write JavaScript for loops and then talk about the alternate style and benefits that Lodas

hdu3853——LOOPS

LOOPS Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others) Total Submission(s): 2707    Accepted Submission(s): 1119 Problem Description Akemi Homura is a Mahou Shoujo (Puella Magi/Magical Girl). Homura wants to help

ansible(七)Conditionals 和 loops,自定义loops插件

ansible的Conditionals 和 loops 和salt-stack比起来真的太强大了.   salt-stack使用判断循环,好像,只能使用模板里面的判断和循环. 而ansible本身自带了适用于各种场景的判断循环插件.. 不但如此,像loops,我们还可以自己编写插件,满足我们的实际需求. 先说说Conditionals吧 Conditionals相对loops来说,简单,就一个关键字when 和咱们其他的程序语言的条件判断一个样,when 后面也是加一个bool值,或者bool

NESTED LOOPS &amp; HASH JOIN &amp; SORT MERGE JOIN

表连接方式及使用场合 NESTED LOOP 对于被连接的数据子集较小的情况,nested loop连接是个较好的选择.nested loop就是扫描一个表,每读到一条记录,就根据索引去另一个表里面查找,没有索引一般就不会是 nested loops.一般在nested loop中, 驱动表满足条件结果集不大,被驱动表的连接字段要有索引,这样就走nstedloop.如果驱动表返回记录太多,就不适合nested loops了.如果连接字段没有索引,则适合走hash join,因为不需要索引. 可用

hdu3853 LOOPS

HDU - 3853 LOOPS 题意:r*c的网格,人在左上角,出口在右下角 人有三种选择,不走,向下走,向右走,每种选择都有一定的概率,每种选择都会花费2魔力值 问到达出口的期望魔力值 /* dp[i][j]表示从(i,j)到出口的期望值 */ #include<iostream> #include<cstdio> #include<cstring> using namespace std; int n,m; double dp[1010][1010],v[1010

HDU 3853:LOOPS

LOOPS 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3853 题意: 有一个网格,从点(1,1)开始走,要走到点(n,m),点(X,Y)可以走到点(X,Y),(X,Y+1),(X+1,Y),概率分别为p0,p1,p2,每走一步耗费2点魔力,求走到终点所耗费的魔力的期望 题解: 一道简单的求期望的题,不会求期望的可以看下这里 具体公式如下: dp[i][j]=p[i][j][0]*dp[i][j]+p[i][j][1]*dp[i][j+1]+

HDU 3853 LOOPS (概率dp)

LOOPS Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others) Total Submission(s): 2931    Accepted Submission(s): 1209 Problem Description Akemi Homura is a Mahou Shoujo (Puella Magi/Magical Girl). Homura wants to help