How can the timeout parameter in socket_select() be utilized effectively to manage non-blocking sockets in PHP?

When working with non-blocking sockets in PHP, the timeout parameter in socket_select() can be utilized effectively to manage the sockets by specifying the maximum amount of time to wait for activity on the sockets before the function returns. This allows for efficient handling of multiple sockets without blocking the script.

// Create an array of sockets to monitor
$read = array($socket1, $socket2);

// Set the timeout value in microseconds (e.g. 500 milliseconds)
$timeout = 500000;

// Use socket_select() to wait for activity on the sockets
if (socket_select($read, $write, $except, 0, $timeout) > 0) {
    // Handle activity on the sockets
    foreach ($read as $socket) {
        // Process incoming data or perform other actions
    }
}