When are infinite loops are useful in PHP?

2019-01-18 06:30发布

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

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.

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

12条回答
Anthone
2楼-- · 2019-01-18 06:55

Infinite loops are particulary useful when creating command line applications. The application will then run until the user tells it to stop. (add a break/exit-statement when the user input is "quit", for instance)

while (true) {
  $input = read_input_from_stdin();

  do_something_with_input();

  if ($input == 'quit') {
    exit(0);
  }
}
查看更多
做自己的国王
3楼-- · 2019-01-18 06:56

Sometimes instead of a loop which would have an exit condition too long to preserve readability an improperly named "infinite" loop may be the best way to go.

<?php

while(1) {
  // ... 
  $ok=preg_match_all('/.../',$M,PREG_SET_ORDER);
  if (!$ok) break;

  switch(...) { ... }

  // match another pattern
  $ok=preg_match('/.../',$M,PREG_SET_ORDER);
  if (!$ok) break;

  //and on, and on...
}
查看更多
成全新的幸福
4楼-- · 2019-01-18 06:58

Maybe it useful when you write a command line PHP application? Because when PHP-scripts runs by web-server (Apache or any other) they lifetime is limited by 30 seconds by default (or you can manually change this limit at the configuration file).

查看更多
相关推荐>>
5楼-- · 2019-01-18 07:00

For user input...

while True:
    input = get_input_from_user("Do you want to continue? ")
    if input not in ("yes", "y", "no", "n"):
        print "invalid input!"
    else: 
        break
查看更多
做个烂人
6楼-- · 2019-01-18 07:01

Monitoring applications

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.)

Server listening for Clients

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.

Video Games

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.

查看更多
成全新的幸福
7楼-- · 2019-01-18 07:03

I'm thinking about the guess-the-number game, where the user has to guess the randomly (or not) generated number, so, he'll have to enter numbers until he gets it. Is that what you needed ?

查看更多
登录 后发表回答