Home >Backend Development >PHP Tutorial >Learn how to write and function PHP infinite loops
Many novices often accidentally write loops into infinite loops when they first start writing PHP, but sometimes making good use of infinite loops, PHP's infinite loops can help us solve many problems. question.
Related learning recommendations: PHP programming from entry to proficiency
The simplest way to write an infinite loop
while (true) { // 这里可以写循环中执行操作 }
Infinite loop writing method two
do { //要执行的代码; } while (true);
Infinite loop writing method three
for($i=1;i>0;i++){ // 这里可以写循环中执行操作 }
The above writing method is because $i
default It is greater than 0, so $i
is executed, so the loop inside is always true, so it is an infinite loop
Usage scenario
We are here Read or execute some data through an infinite loop, such as an infinite loop to perform operations such as redis data update
Terminate the loop
Through break;
The method can break out of the infinite loop
Functions commonly used in infinite loop tubes
sleep() //主要是让死循环得到休息,不至于崩溃。 set_time_limit(0); //设置执行最长时间,0为无限制。 ignore_user_abort(true); //关闭浏览器,服务器也能自动执行。 break; //跳出循环
The above is the detailed content of Learn how to write and function PHP infinite loops. For more information, please follow other related articles on the PHP Chinese website!