Deep understanding of PHP looping statements

Source: Internet
Author: User
Loop structure
one, while Loop

while (expression) {  loop-body;//execute repeatedly until expression is false}


$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

Second, do and loop

Do {      loop-body;//execute repeatedly until expression is false} while (expression)


Do {         $index + +;         Print "number is {$index}";} while ($index <0);p rint ' done ';

run Result:
Number is 1

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) {

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


foreach (array as value) {    code to is executed;}


<?php$arr=array ("One", "one", "one", "three"), foreach ($arr as $value) {  echo "value:". $value. "";}? >

Operation Result:
The above is PHP in four kinds of circulating body, according to different conditions to choose the appropriate circulation body application

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.