PHP looping statements in detail while, for, foreach, do while_php tutorials

Source: Internet
Author: User

PHP looping statements in detail while, for, foreach, does while


One, while loop while (expression) {loop body;//execute repeatedly until expression is false} code: $index = 1; while ($index <5) {print ' number is {$index} '; $index + +; The result of the operation: number is 1 number is 2 number is 3 number is 42, does while loop do {loop body;//execute repeatedly until expression is false} while (expression) code: do {$index + +; Print "number is {$index}";} while ($index <0); Running Result: number is 1 does while loop statement is a certain difference from while, the difference between do and whether the condition is true or not, and the while must be true before it is executed once. For a For loop, there are two types of loops depending on the loop condition: counting loops (typically using for) another: conditional loops (typically using while do-while) for (EXPR1; expr2; expr3) {statement} EX PR1 is the initial value of the condition. EXPR2 is a condition for judging, usually using the logic operation symbol (logical operators) 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. And statement is a qualifying execution part of the program, if the program has only one row, you can omit the curly brace {}. The following example is a "dare not" example written with a for loop, which can be compared with a while loop. "; } ? > Operation Result: 1. Later dared not 2. Later dared not 3. Later dared not 4. Later dared not 5. Later dared not 6. Later dared not 7. Later dared not 8. Later dared not 9. Later dared not 10. Not anymore. Four, the Foreach Loop 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 be executed;} code: Operation Result: Value:one value:two value:three above is php four kinds of circulation body, according to different conditions to choose the corresponding circulation body application

http://www.bkjia.com/PHPjc/1065651.html www.bkjia.com true http://www.bkjia.com/PHPjc/1065651.html techarticle The PHP Loop statement is detailed while, for, foreach, does while a while loop while (expression) {loop body;//executes repeatedly until the expression is false} code: $index = 1; while ($index 5) {. ..

  • 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: info-contact@alibabacloud.com 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.