什么时候无限循环在PHP中有用? [英] When are infinite loops are useful in PHP?

查看:97
本文介绍了什么时候无限循环在PHP中有用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读他所说的Paul Hudson出色的在线 PHP 教程时

While reading through the great online PHP tutorials of Paul Hudson he said

也许令人惊讶的是,无限循环 有时对您有帮助 脚本.作为永无止境的循环 在没有外界影响的情况下终止, 最受欢迎的使用方式是 打破循环和/或退出 脚本完全来自循环内 只要条件匹配.你 也可以依靠用户输入 终止循环-例如,如果 您正在编写一个程序来接受 人们输入数据的时间长达 他们想要的,这只是行不通的 脚本循环30,000次或 甚至3亿次.相反, 代码应该永远不断循环 接受用户输入直到用户 通过按Ctrl-C结束程序.

Perhaps surprisingly, infinite loops can sometimes be helpful in your scripts. As infinite loops never terminate without outside influence, the most popular way to use them is to break out of the loop and/or exit the script entirely from within the loop whenever a condition is matched. You can also rely on user input to terminate the loop - for example, if you are writing a program to accept people typing in data for as long as they want, it just would not work to have the script loop 30,000 times or even 300,000,000 times. Instead, the code should loop forever, constantly accepting user input until the user ends the program by pressing Ctrl-C.

请给我一个简单的运行示例,说明如何在PHP中使用无限循环吗?

Would you please give me a simple running example of how to use infinite loops in PHP ?

推荐答案

监视应用程序

如果您有一个后台进程来监视服务器的状态并在出现问题时发送电子邮件.它将有一个无限循环来反复检查服务器(迭代之间有一些暂停).

If you have a background process that monitors the state of your servers and sends an email if trouble occurs. It would have an infinite loop to repeatably check the servers (with some pause between iterations.)

服务器正在监听客户端

如果您的服务器脚本侦听套接字的连接,它将无限循环,从而在等待新客户端连接时阻塞.

If you have a server script that listens to a socket for connections, it will loop infinitely, blocking while waiting for new clients to connect.

视频游戏

游戏通常有一个游戏循环",无限期地运行一次.

Games usually have a "game loop" that runs once a frame, indefinitely.

或者...需要通过定期检查在后台继续运行的任何其他东西.

Or... anything else that needs to keep running in the background with periodic checks.

这篇关于什么时候无限循环在PHP中有用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆