In PHP, how can a stream be effectively monitored to determine when it has completed its operation?

When monitoring a stream in PHP to determine when it has completed its operation, you can utilize the `stream_select` function to check if the stream is ready for reading or writing. By using this function in combination with a timeout value, you can effectively monitor the stream and take appropriate actions once it has completed its operation.

$stream = fopen('example.txt', 'r');
$read = [$stream];
$write = null;
$except = null;

if (stream_select($read, $write, $except, 0) === 1) {
    // Stream is ready for reading, operation has completed
    // Add your code here to handle the completion of the stream operation
} else {
    // Stream is still in progress
    // Add your code here to continue monitoring the stream
}

fclose($stream);