PHP Function Reference

PHP stream_select() Function



The PHP stream_select() function accepts arrays of streams with a timeout specified by tv_sec and tv_usec and waits for them to change status.

Syntax

stream_select(read, write, except, tv_sec, tv_usec)

Parameters

read Required. The streams listed in the read array will be watched to see if characters become available for reading (more precisely, to see if a read will not block - in particular, a stream resource is also ready on end-of-file, in which case an fread() will return a zero length string).
write Required. The streams listed in the write array will be watched to see if a write will not block.
except

Required. The streams listed in the except array will be watched for high priority exceptional ("out-of-band") data arriving.

Note: When stream_select() returns, the arrays read, write and except are modified to indicate which stream resource(s) actually changed status. The original keys of the arrays are preserved.
tv_sec

Required. The tv_sec and tv_usec together form the timeout parameter, tv_sec specifies the number of seconds while tv_usec the number of microseconds. The timeout is an upper bound on the amount of time that this function will wait before it returns. If tv_sec and tv_usec are both set to 0, this function will not wait for data - instead it will return immediately, indicating the current status of the streams.

If tv_sec is null stream_select() can block indefinitely, returning only when an event on one of the watched streams occurs (or if a signal interrupts the system call).
tv_usec Optional. See tv_sec description.

Note: Using a timeout value of 0 allows to instantaneously poll the status of the streams, however, it is NOT recommended to use a timeout value 0 in a loop as it will cause the script to consume too much CPU time.

It is much better to specify a timeout value of a few seconds, although if it is required to be checking and running other code concurrently, using a timeout value of at least 200000 microseconds will help to reduce the CPU usage of the script.

Note that the timeout value is the maximum time that will elapse. The stream_select() function will return as soon as the requested streams are ready for use.

Return Value

On success this function returns the number of stream resources contained in the modified arrays, which may be zero if the timeout expires before anything interesting happens. On error false is returned and a warning raised (this can happen if the system call is interrupted by an incoming signal).

Example: stream_select() example

The example below checks to see if data has arrived for reading on either $stream1 or $stream2. Since the timeout value is 0 it will return immediately.

<?php
//preparing the read array
$read = array($stream1, $stream2);
$write = NULL;
$except = NULL;

if (false === ($num_changed_streams = 
      stream_select($read, $write, $except, 0))) {
  //error handling codes
} elseif ($num_changed_streams > 0) {
  //at least on one of the streams 
  //something interesting happened
}
?>

Note: Due to a limitation in the current Zend Engine it is not possible to pass a constant modifier like null directly as a parameter to a function which expects this parameter to be passed by reference. Instead use a temporary variable or an expression with the leftmost member being a temporary variable:

<?php
$e = NULL;
stream_select($r, $w, $e, 0);
?>

Note: Use the === operator when checking for an error. Since the stream_select() may return 0 the comparison with == would evaluate to true.

❮ PHP Streams Reference