Home PHP Stream stream_select() PHP stream_select() Function Definition In PHP, stream_select() waits until one or more streams are ready to read, write, or report an exceptional condition, or until a timeout expires.
It is the stream counterpart of socket_select(). You pass arrays of streams by reference; PHP rewrites those arrays so only the ready streams remain, and returns how many streams were ready.
Syntax Call stream_select() with the values you need. Start with this line.
$n = stream_select($read, $write, $except, 0); Call What it does stream_select($read, $write, $except, 0)Polls once with a zero-second timeout and returns the number of ready streams. stream_select($read, $write, $except, 5)Waits up to 5 seconds for activity. stream_select($read, $write, $except, null)Waits indefinitely until at least one stream is ready.
You must pass variables for the array arguments because stream_select() modifies them in place. Use null for a set you do not care about. Tutorial output below is a fixed illustration using a readable memory stream.
Library declarations stream_select(?array &$read, ?array &$write, ?array &$except, ?int $seconds, ?int $microseconds = null): int|false Parameters Parameter Description read Optional by reference. Streams to watch for reading; replaced with the ready subset. write Optional by reference. Streams to watch for writing; replaced with the ready subset. except Optional by reference. Streams to watch for exceptional conditions. seconds Timeout seconds. null means wait forever; 0 means poll without waiting. microseconds Optional extra timeout in microseconds.
Return Value Return Value Description int The number of ready streams contained in the modified arrays. false Returned on error (for example an interrupted system call on some platforms).
PHP Examples 1. Poll a Readable Memory Stream 2. Timeout With Nothing to Watch for Write 3. Arrays Are Filtered to Ready Streams Example 1 — Poll a Readable Memory Stream Load data into php://memory, rewind, then use stream_select() with a zero timeout. The numbers below are the fixed tutorial result.
<?php
$stream = fopen("php://memory", "r+");
fwrite($stream, "ready");
rewind($stream);
$read = [$stream];
$write = null;
$except = null;
$n = stream_select($read, $write, $except, 0);
echo "ready count: ", $n, PHP_EOL;
echo "data: ", stream_get_contents($read[0]), PHP_EOL;
fclose($stream); ready count: 1
data: ready How It Works A memory stream is filled with the word ready and rewound. That stream is placed in the $read array watched by stream_select(). With a 0-second timeout, PHP reports 1 ready stream in this illustration. Reading from the surviving entry in $read prints the stored text. Example 2 — Timeout With Nothing to Watch for Write Watch only an empty write set conceptually by passing null arrays you do not need, and show a zero-timeout poll shape.
<?php
$stream = fopen("php://temp", "r+");
$read = [$stream];
$write = null;
$except = null;
$n = stream_select($read, $write, $except, 0, 0);
echo "returned int: ", is_int($n) ? "yes" : "no", PHP_EOL;
echo "n >= 0: ", $n >= 0 ? "yes" : "no", PHP_EOL;
fclose($stream); returned int: yes
n >= 0: yes How It Works A temp stream is added to the read set. write and except are null because we are not watching those sets. stream_select() returns an integer count on success. The demo only asserts that the return value is a non-negative int. Frequently Asked Questions What does stream_select() do in PHP? It waits until streams are ready to read or write (or until a timeout), then leaves only the ready streams in the arrays you passed by reference.
How is stream_select() like socket_select()? Both multiplex many I/O handles in one wait call. stream_select() works with PHP stream resources; socket_select() works with sockets.
Why must the array arguments be variables? Because stream_select() modifies them in place. Literals cannot be passed by reference.
What does a return value of 0 mean? The timeout expired without any watched stream becoming ready.
Should I combine this with stream_set_blocking()? Yes, for non-blocking event loops. Set blocking to false, then use stream_select() to wait until a stream is ready.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful
Helpful Share Copy link Suggestion