Example of a "while" loop statement in a PHP loop control statement

Source: Internet
Author: User
In our program development, it may be necessary to repeat the use of a piece of code or a function, such as the need to enter "1*2*3*4...*100", if the human input, it will waste a lot of time, but also very cumbersome, but in this case, there is a good way, is to use our PHP loop control statements, the use of PHP loop control statements, you can quickly complete the calculation, in PHP, we provide four kinds of loop control statements: While, Do...while, for, and foreach. This chapter, first of all, to explain the first circular control statement the "while" loop statement.

Syntax format for "while" loop statement

The while loop is the simplest looping statement in PHP

while (expr) {           statement;}

Syntax Explanation:

Structurally, as with an if statement, it also relies on a condition, but the difference is that the IF statement executes the statement only when expr is true, while the while statement repeats statement as long as expr is true. The loop is not popped until the value of the expression is false.

If you do not give the while out of the loop condition, then the expression expr is always true, the loop will be executed forever, which forms a dead loop, which is not allowed by our program. Think of code like the following

<?phpwhie (1) {    echo 1111. ' <br/> ';}? >

The while condition is 1, which is always true, so it will continue to loop, is a dead loop, causing the page to crash

While loop instance

This example is to achieve an even output within 10, from 1~10 to determine whether an even number, if it is, then the output, if not, then continue the next loop, the code is as follows

<?phpheader ("Content-type:text/html;charset=utf-8");    Set the encoding $num=1;                             Declare an integer variable $sum$str= "an even number within 10 is:";            The above string variable $strwhile ($num <=10) {                    //determines if the variable $num is less than the If    ($num%2==0) {                  ///if less than 10, determines whether $num is an even        $str. = $num. " ";              If the variable is an even number, it is added to the character variable $str after    }    $num + +;                         Variable $num plus 1}echo $str;                          Loop end, output string $str?>

Code Execution Results:

Above is just a simple while loop example, in order to skillfully use the Whlie loop in the project, you can experience the actual development process in the future. Next section, for everyone to explain the "do...while" loop statement.

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