什么时候无限循环在PHP中有用?

taa*_*eid 7 php infinite-loop

在阅读Paul Hudson 的精彩在线PHP教程时,他说

也许令人惊讶的是,无限循环有时会对您的脚本有所帮助.由于无限循环永远不会在没有外部影响的情况下终止,因此最常用的方法是在条件匹配时从循环内完全退出循环和/或退出脚本.您还可以依赖用户输入来终止循环 - 例如,如果您正在编写程序以接受人们根据需要输入数据,那么脚本循环30,000次甚至300,000,000次都是行不通的.相反,代码应该永远循环,不断接受用户输入,直到用户按Ctrl-C结束程序.

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

Ben*_*n S 15

监控应用程序

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

服务器侦听客户端

如果您有一个服务器脚本侦听套接字以进行连接,它将无限循环,在等待新客户端连接时阻塞.

视频游戏

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

或者......需要定期检查后在后台继续运行的任何其他内容.

  • @Sergey:当然,为什么不呢?PHP有一些图形库.如果你愿意,你可以用PHP编写游戏.我提到它是因为它是无限循环的经典例子. (6认同)
  • 您是否看到使用PHP语言编写的视频游戏? (5认同)
  • 我认为PHP不适合实时应用程序.但我读过一次,如果你想使用它,没有什么可以阻止你! (2认同)

AJ.*_*AJ. 5

如果您实现了套接字服务器(取自:http://devzone.zend.com/article/1086):

    #!/usr/local/bin/php –q

<?php
// Set time limit to indefinite execution
set_time_limit (0);

// Set the ip and port we will listen on
$address = '192.168.0.100';
$port = 9000;
$max_clients = 10;

// Array that will hold client information
$clients = Array();

// Create a TCP Stream socket
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
// Bind the socket to an address/port
socket_bind($sock, $address, $port) or die('Could not bind to address');
// Start listening for connections
socket_listen($sock);

// Loop continuously
while (true) {
    // Setup clients listen socket for reading
    $read[0] = $sock;
    for ($i = 0; $i < $max_clients; $i++)
    {
        if ($client[$i]['sock']  != null)
            $read[$i + 1] = $client[$i]['sock'] ;
    }
    // Set up a blocking call to socket_select()
    $ready = socket_select($read,null,null,null);
    /* if a new connection is being made add it to the client array */
    if (in_array($sock, $read)) {
        for ($i = 0; $i < $max_clients; $i++)
        {
            if ($client[$i]['sock'] == null) {
                $client[$i]['sock'] = socket_accept($sock);
                break;
            }
            elseif ($i == $max_clients - 1)
                print ("too many clients")
        }
        if (--$ready <= 0)
            continue;
    } // end if in_array

    // If a client is trying to write - handle it now
    for ($i = 0; $i < $max_clients; $i++) // for each client
    {
        if (in_array($client[$i]['sock'] , $read))
        {
            $input = socket_read($client[$i]['sock'] , 1024);
            if ($input == null) {
                // Zero length string meaning disconnected
                unset($client[$i]);
            }
            $n = trim($input);
            if ($input == 'exit') {
                // requested disconnect
                socket_close($client[$i]['sock']);
            } elseif ($input) {
                // strip white spaces and write back to user
                $output = ereg_replace("[ \t\n\r]","",$input).chr(0);
                socket_write($client[$i]['sock'],$output);
            }
        } else {
            // Close the socket
            socket_close($client[$i]['sock']);
            unset($client[$i]);
        }
    }
} // end while
// Close the master sockets
socket_close($sock);
?> 
Run Code Online (Sandbox Code Playgroud)