Loop structure
one, while Loop
while (expression) { loop-body;//execute repeatedly until expression is false}
Code:
$index = 1;while ($index <5) { print "number is {$index}"; $index + +;} print "Done";
Operation Result:
Number is 1
Number is 2
Number is 3
Number is 4
Done
Second, do and loop
Do { loop-body;//execute repeatedly until expression is false} while (expression)
Code:
Do { $index + +; Print "number is {$index}";} while ($index <0);p rint ' done ';
run Result:
Number is 1
did
do While loop statement is different from while, the difference between do While the condition is true, it executes first, while the while must be true to execute once.
three, for Loop
Depending on the loop condition, There are two types of loops
one: Count loops (general use for)
Another: Conditional loop (typically using while do-while)
for (expr1; expr2; expr3) {
statement
}
When judging the condition. EXPR3 the part to be executed after the execution of the statement, to change the condition for the next cycle judgment, such as add one. Wait a minute. Instead of statement
<?php for ($i =1; $i <=5; $i + +) { echo "$i." n ";}?>
Operation Result:
1. Not anymore.
2. Not anymore.
3. Not anymore.
4. Not anymore.
5. Not anymore.
Four, foreach Loop
The foreach statement is used to iterate through an array. For Each loop, the value of the current array element is assigned to the value variable (the array pointer moves one at a time)-and so on
Grammar:
foreach (array as value) { code to is executed;}
Code:
<?php$arr=array ("One", "one", "one", "three"), foreach ($arr as $value) { echo "value:". $value. "";}? >
Operation Result:
Value:one
Value:two
Value:three
The above is PHP in four kinds of circulating body, according to different conditions to choose the appropriate circulation body application