PHP stream_set_blocking() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Stream

Definition

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

In blocking mode (the default for many streams), reads wait until data arrives. In non-blocking mode, reads return immediately when no data is available, which pairs well with stream_select() in network code.

Syntax

Call stream_set_blocking() with the values you need. Start with this line.

PHP
$ok = stream_set_blocking($stream, false);
CallWhat it does
stream_set_blocking($stream, true)Enables blocking mode so reads can wait for data.
stream_set_blocking($stream, false)Enables non-blocking mode so reads return at once when idle.
stream_set_blocking($stream, 0)Also disables blocking; PHP treats 0 as false.

Not every wrapper honors non-blocking mode the same way. Socket streams are the usual audience. Always check the boolean return value when mode changes matter.

Library declarations
PHP
stream_set_blocking(resource $stream, bool $enable): bool

Parameters

ParameterDescription
streamRequired. The stream to configure.
enableRequired. true for blocking mode, false for non-blocking mode.

Return Value

Return ValueDescription
booltrue on success; false on failure.

PHP Examples

Example 1 — Toggle Blocking on a Memory Stream

Call stream_set_blocking() both ways and print whether each call succeeded.

PHP
<?php
$stream = fopen("php://memory", "r+");

$a = stream_set_blocking($stream, true);
$b = stream_set_blocking($stream, false);

echo "blocking on: ", $a ? "ok" : "fail", PHP_EOL;
echo "blocking off: ", $b ? "ok" : "fail", PHP_EOL;
fclose($stream);

How It Works

  1. A memory stream is opened.
  2. stream_set_blocking($stream, true) turns blocking on.
  3. stream_set_blocking($stream, false) turns it off.
  4. Both calls return true in this tutorial, printed as ok.

Example 2 — Read Metadata After a Mode Change

After disabling blocking, inspect the blocked metadata flag.

PHP
<?php
$stream = fopen("php://temp", "r+");
stream_set_blocking($stream, false);
$meta = stream_get_meta_data($stream);

echo "blocked=", $meta["blocked"] ? "yes" : "no", PHP_EOL;
fclose($stream);

How It Works

  1. The stream is switched to non-blocking mode.
  2. stream_get_meta_data() includes a blocked key.
  3. After a successful disable, blocked is false.
  4. The script prints blocked=no.

Example 3 — Prepare a Stream for select()-Style Loops

Show the usual setup order: open, set non-blocking, then mention select.

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

stream_set_blocking($stream, false);
$read = [$stream];
$write = null;
$except = null;
$n = stream_select($read, $write, $except, 0);

echo "select ready: ", $n, PHP_EOL;
fclose($stream);

How It Works

  1. Data is written so the stream has something to read.
  2. stream_set_blocking() disables blocking before multiplexing.
  3. stream_select() polls the read set with a zero timeout.
  4. This illustration reports 1 ready stream.

Frequently Asked Questions

It turns blocking mode on or off for a stream. Blocking waits for I/O; non-blocking returns immediately when no data is ready.
When you juggle several sockets or streams and do not want one slow peer to freeze the whole script. Combine it with stream_select().
Many streams start in blocking mode. Network code often switches to non-blocking explicitly.
Support varies. It is most important for socket-based streams. Always check the return value.
Call stream_get_meta_data() and read the blocked entry after you change the mode.

Next: stream_set_chunk_size()

stream_set_chunk_size() sets how many bytes PHP prefers to read or write in one chunk on t.

stream_set_chunk_size() 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