PHP loop statement usage-while loop

Source: Internet
Author: User
One thing computers are very good at is to execute tasks automatically and repeatedly. If some tasks need to be executed multiple times in the same way, you can use loop statements to repeat some parts of the program. In

One thing computers are very good at is to execute tasks automatically and repeatedly. If some tasks need to be executed multiple times in the same way, you can use loop statements to repeat some parts of the program. In PHP, the simplest loop is the While loop. Like an if statement, it also depends on a condition. The While loop statement differs from the if statement because the if statement executes subsequent code blocks only once when the condition is true, While the While loop statement only has the condition true, the code block will be repeatedly executed.

When we do not know the required number of repetitions, we can use the while loop statement. If you require a fixed number of duplicates, you can use the for loop statement. The basic structure of the While loop statement is as follows:
While (condition) execution statement;
The while loop statement shown below shows numbers 0 ~ 5.
$ Num = 0;
While ($ num <= 5 ){
Echo $ num ."
";
$ Num ++;
}

Conditions are tested at the beginning of each iteration. If the condition is false, the statement block is not executed and the loop ends. The next statement after the loop statement is executed. We can use the while loop to complete some more meaningful tasks, such as the freight calculation table (the shipping distance is greater than or equal to 50 and the total freight is increased by 5 for each increase of 50 ), is implemented using the while.

While loop statement

The key code is as follows:
$ Distance = 50;
While ($ distance <= 250 ){
Echo"
". $ Distance ."
". ($ Distance/10 )."
";
$ Distance + = 50;
}
?>

Of course, the above is just a simple example. if you want to skillfully use the whlie loop statement in the project, you can experience it in the actual development process in the future.

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.