In PHP, stream_select() is useful when your application needs a specific operation rather than manually reproducing it. Waits until selected stream resources are ready for reading, writing or exceptional conditions, with an optional timeout.
Stream functions work with PHP stream resources, wrappers, contexts or custom filters. Not every wrapper supports every operation.
What Is stream_select() in PHP?
Waits until selected stream resources are ready for reading, writing or exceptional conditions, with an optional timeout.
Signature:
stream_select(?array $read, ?array $write, ?array $except, ?int $seconds, ?int $microseconds = NULL): int|false
Parameters:
$read— Array of readable stream resources passed by reference, modified to contain the ready streams.$write— Array of writable stream resources passed by reference, modified to contain the ready streams.$except— Array of streams to check for exceptional conditions, passed by reference.$seconds— Maximum whole seconds to wait, unless the API specifies otherwise.$microseconds— Additional timeout portion in microseconds.
Return value: int|false.
Example 1: Basic Usage
This first example shows the method or function in a minimal context. For environment-dependent operations, adapt the required path, stream or connection before running it.
<?php
$socketPair = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
fwrite($socketPair[0], "ping");
$read = [$socketPair[1]];
$write = $except = [];
$ready = stream_select($read, $write, $except, 1);
if ($ready > 0) { echo fread($read[0], 4); }
fclose($socketPair[0]); fclose($socketPair[1]);
The important operation is stream_select(). Waits until selected stream resources are ready for reading, writing or exceptional conditions, with an optional timeout.
Example 2: Another Input or Scenario
The next example changes the input or surrounding context to show how the operation behaves in a second situation.
<?php
$pair = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
$read = [$pair[1]];
$write = $except = [];
$readyCount = stream_select($read, $write, $except, 0, 200000);
if ($readyCount === false) { throw new RuntimeException("Select failed"); }
echo $readyCount === 0 ? "Timed out" : "Ready";
fclose($pair[0]); fclose($pair[1]);
Compare the inputs and the handling of the return value with the first example. Avoid assuming that filesystem paths, resource states or binary bytes are interchangeable.
Practical Example: Applying the Operation
This example illustrates how the operation fits into a small application workflow. Paths, streams and secrets must be created or provided securely by the calling application.
<?php
$pair = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
fwrite($pair[0], "Report ready");
$readStreams = [$pair[1]];
$writeStreams = $exceptStreams = [];
$readyCount = stream_select($readStreams, $writeStreams, $exceptStreams, 2);
if ($readyCount > 0) { foreach ($readStreams as $readyStream) { echo fread($readyStream, 1024); } }
fclose($pair[0]); fclose($pair[1]);
Practical note: Waits until selected stream resources are ready for reading, writing or exceptional conditions, with an optional timeout.
Important Behavior and Edge Cases
- Waits until selected stream resources are ready for reading, writing or exceptional conditions, with an optional timeout.
- Check failure values and exceptions; do not assume every environment supports the same stream wrapper, extension or filesystem capability.
- When handling paths, streams or external input, validate inputs before using results and release resources after use.
Comparison With Related Functions
| Function or Method | Behavior |
|---|---|
stream_select() | waits for readiness of multiple streams. |
stream_set_blocking() | changes blocking behavior of one stream. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
Assuming stream_select() can be used without its required setup | Provide the correct file, stream, state, key or object before calling it; Waits until selected stream resources are ready for reading, writing or exceptional conditions, with an optional timeout. |
| Assuming one socket read returns the whole message | Treat socket input as a stream and frame messages explicitly; read until a known boundary or length. |
| Omitting timeouts and failure handling | Check false results, timeouts, and partial writes; close sockets on every error path. |
Key Takeaways
stream_select()waits until selected stream resources are ready for reading, writing or exceptional conditions, with an optional timeout.- Use appropriate input types and verify results before continuing.
- Prefer specialized, safer APIs when the operation concerns untrusted input, encryption or network traffic.
In summary: Waits until selected stream resources are ready for reading, writing or exceptional conditions, with an optional timeout. Build in error handling and use this function where its specific behavior matches the application requirements.