码迷,mamicode.com
首页 > Web开发 > 详细

For loops in PHP

时间:2015-10-08 20:05:34      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:

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!

A 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. A 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.

A 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.

 

For loops in PHP

标签:

原文地址:http://www.cnblogs.com/elewei/p/4862078.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!