Use of loop statements in PHP process control
Use of loop statements
Student Wang needs to travel back and forth between Beijing and Dalian repeatedly, which is a typical loop structure. Assume that Mr. Wang Si’s investment in this project requires 100 round trips to Dalian, and Mr. Wang will count each round trip. Should we write the same code a hundred times? Obviously it is impossible for programmers with extremely high IQs to handle this.
We abstracted this kind of human thinking. We define a loop structure
<?php //定義需要往返的次數(shù),老外喜歡從0開始計數(shù),我們也從0開始計 $count = 0; //while后面接布爾值判斷,為真執(zhí)行,為假停止 //$count 小于100的時候執(zhí)行,也就是$count為0至99的時候執(zhí)行 //如果$count不小于100了,循環(huán)停止執(zhí)行后續(xù)的代碼 //循環(huán)開始處 while($count < 100){ echo '我是王思總,我是第' . $count .'次出差<br />'; //每次執(zhí)行讓$count+1,這樣的話,就不會產(chǎn)生$count永遠(yuǎn)小于100的情況了 $count++; //循環(huán)結(jié)束 } echo '后續(xù)代碼'; ?>
We can add a special code logic diagram for the while loop: