stream_select()

stream_select() — Runs the equivalent of the select() system call on the given arrays of streams with a timeout specified by seconds and microseconds.

Built-in function of PHP 8.5. The manual lists it as available in PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

What it does

The stream_select() function accepts arrays of streams and waits for them to change status. It is the stream counterpart of socket_select(), but the two do not behave alike in every respect: they report invalid arguments differently, and stream_select() can return without consulting the operating system at all, as described in the notes below.

Signature

stream_select(?array &$read, ?array &$write, ?array &$except, ?int $seconds, ?int $microseconds = null): int|false

Parameters

ParameterTypeWhat it takes
&$read?arrayThe 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?arrayThe streams listed in the write array will be watched to see if a write will not block.
&$except?arrayThe streams listed in the except array will be watched for high priority exceptional ("out-of-band") data arriving.
$seconds?intThe seconds and microseconds together form the timeout parameter, seconds specifies the number of seconds while microseconds the number of microseconds. The timeout is an upper bound on the amount of time that stream_select() will wait before it returns. If seconds is set to 0 and microseconds is 0 or null, stream_select() will not wait for data - instead it will return immediately, indicating the current status of ...
$microseconds?intSee seconds description.

Return value

On success stream_select() 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

A runnable snippet:

/* Prepare the read array */ $read = array($stream1, $stream2); $write = NULL; $except = NULL; if (false === ($num_changed_streams = stream_select($read, $write, $except, 0))) { /* Error handling */ } elseif ($num_changed_streams > 0) { /* At least on one of the streams something interesting happened */ }

Changelog

VersionChange
8.1.0microseconds is now nullable.

See also

stream_set_blocking()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page stream_select() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.