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.