PHP stream_select() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Stream

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.

PHP
$n = stream_select($read, $write, $except, 0);
CallWhat 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
PHP
stream_select(?array &$read, ?array &$write, ?array &$except, ?int $seconds, ?int $microseconds = null): int|false

Parameters

ParameterDescription
readOptional by reference. Streams to watch for reading; replaced with the ready subset.
writeOptional by reference. Streams to watch for writing; replaced with the ready subset.
exceptOptional by reference. Streams to watch for exceptional conditions.
secondsTimeout seconds. null means wait forever; 0 means poll without waiting.
microsecondsOptional extra timeout in microseconds.

Return Value

Return ValueDescription
intThe number of ready streams contained in the modified arrays.
falseReturned on error (for example an interrupted system call on some platforms).

PHP Examples

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
<?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);

How It Works

  1. A memory stream is filled with the word ready and rewound.
  2. That stream is placed in the $read array watched by stream_select().
  3. With a 0-second timeout, PHP reports 1 ready stream in this illustration.
  4. 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
<?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);

How It Works

  1. A temp stream is added to the read set.
  2. write and except are null because we are not watching those sets.
  3. stream_select() returns an integer count on success.
  4. The demo only asserts that the return value is a non-negative int.

Example 3 — Arrays Are Filtered to Ready Streams

Show that after stream_select(), you should read from the streams still present in $read.

PHP
<?php
$a = fopen("php://memory", "r+");
fwrite($a, "A");
rewind($a);

$read = [$a];
$write = null;
$except = null;
stream_select($read, $write, $except, 0);

echo "streams left: ", count($read), PHP_EOL;
echo "first byte: ", fread($read[0], 1), PHP_EOL;
fclose($a);

How It Works

  1. One prepared memory stream is watched for reading.
  2. stream_select() keeps ready streams inside $read.
  3. count($read) is 1 in this fixed illustration.
  4. fread() on $read[0] returns the first stored byte A.

Frequently Asked Questions

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.
Both multiplex many I/O handles in one wait call. stream_select() works with PHP stream resources; socket_select() works with sockets.
Because stream_select() modifies them in place. Literals cannot be passed by reference.
The timeout expired without any watched stream becoming ready.
Yes, for non-blocking event loops. Set blocking to false, then use stream_select() to wait until a stream is ready.

Next: stream_set_blocking()

stream_set_blocking() switches a stream between blocking mode and non-blocking mode.

stream_set_blocking() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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