stream_filter_append

stream_filter_append(resource $stream, string $filter_name, int $mode = 0, mixed $params = null): resource|false

Stream filter chains: attaches named filters (string.toupper, convert.base64-encode, ...) to a stream's read or write chain so all subsequent reads/writes are transformed automatically (read chain = transform on the way in, write chain = transform on the way out) — a PHP-unique stream decoration mechanism; mode can force READ/WRITE/ALL.

C C23

C23
#include <ctype.h>
#include <stdio.h>

int main(void) {
    FILE *fp = tmpfile();
    fputs("hello codeabc", fp);
    rewind(fp);

    /* C has no stream filters: the transform is applied
     * explicitly while the bytes pass through. */
    int c;
    while ((c = fgetc(fp)) != EOF) {
        putchar(toupper(c));
    }
    putchar('\n');
    return 0;
}
Output
HELLO CODEABC

C++ C++26

C++26
#include <cctype>
#include <cstdio>

int main() {
    FILE *fp = tmpfile();
    fputs("hello codeabc", fp);
    rewind(fp);

    // C++ has no stream filters either: transform explicitly
    // on the way through (or wrap a streambuf for a reusable filter).
    int c;
    while ((c = fgetc(fp)) != EOF) {
        putchar(std::toupper(c));
    }
    putchar('\n');
    return 0;
}
Output
HELLO CODEABC

C# v11.0.100

v11.0.100
using System.Security.Cryptography;
using System.Text;

// CryptoStream is the closest .NET analog: an ICryptoTransform
// chained onto a stream — writes arrive transformed at the other end.
using MemoryStream raw = new();
using (CryptoStream filter = new(raw, new ToBase64Transform(), CryptoStreamMode.Write))
{
    byte[] data = Encoding.ASCII.GetBytes("hello");
    filter.Write(data, 0, data.Length);
    filter.FlushFinalBlock();
}
Console.WriteLine(Encoding.ASCII.GetString(raw.ToArray()));
Output
aGVsbG8=

Go v1.27.4

v1.27.4
package main

import (
	"encoding/base64"
	"fmt"
)

func main() {
	// Go has no stream filters: encode explicitly after reading.
	raw := "hello"
	encoded := base64.StdEncoding.EncodeToString([]byte(raw))
	fmt.Println(encoded)
}
Output
aGVsbG8=

Java v27.0.1

v27.0.1
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.util.Base64;

public class Main {
    public static void main(String[] args) throws Exception {
        // Base64.Encoder.wrap is a true filter analog: bytes written
        // to the wrapper arrive encoded in the underlying stream.
        ByteArrayOutputStream raw = new ByteArrayOutputStream();
        try (OutputStream filter = Base64.getEncoder().wrap(raw)) {
            filter.write("hello".getBytes());
        }
        System.out.println(raw.toString());
    }
}
Output
aGVsbG8=

JavaScript v26.13.1

v26.13.1
const { PassThrough, Transform } = require('node:stream');
const { pipeline } = require('node:stream/promises');

// Node's analog of a filter chain: a Transform stream wired onto
// the flow with pipeline.
async function main() {
  const upper = new Transform({
    transform(chunk, _enc, cb) {
      cb(null, chunk.toString().toUpperCase());
    },
  });
  const src = PassThrough.from('hello codeabc');
  let out = '';
  upper.on('data', (c) => { out += c; });
  await pipeline(src, upper);
  console.log(out);
}
main();
Output
HELLO CODEABC

PHP v8.6.0

v8.6.0
$fp = tmpfile();
// write chain: bytes are uppercased on the way INTO the stream
stream_filter_append($fp, 'string.toupper', STREAM_FILTER_WRITE);
fwrite($fp, 'hello codeabc');
rewind($fp);
echo stream_get_contents($fp), "\n";

$f2 = tmpfile();
fwrite($f2, 'hello');
rewind($f2);
// read chain: raw bytes come back base64-ENCODED on the way OUT
stream_filter_append($f2, 'convert.base64-encode', STREAM_FILTER_READ);
echo stream_get_contents($f2), "\n";
Output
HELLO CODEABC
aGVsbG8=

Python v3.15.2

v3.15.2
import base64

# Python has no stream filters: encode explicitly after reading
# (the codecs module can also wrap streams, but explicit is idiomatic).
raw = b"hello"
print(base64.b64encode(raw).decode())
Output
aGVsbG8=

Rust v1.99.1

v1.99.1
fn main() {
    // Rust std has no stream filters: transform explicitly after
    // reading (the base64 crate covers the encoding equivalent).
    let raw = "hello codeabc";
    let upper: String = raw.to_uppercase();
    println!("{}", upper);
}
Output
HELLO CODEABC

TypeScript v26.13.1

v26.13.1
import { PassThrough, Transform } from 'node:stream';
import { pipeline } from 'node:stream/promises';

// Node's analog of a filter chain: a Transform stream wired onto
// the flow with pipeline.
async function main(): Promise<void> {
  const upper: Transform = new Transform({
    transform(chunk: Buffer, _enc: string, cb: (e: Error | null, d?: string) => void) {
      cb(null, chunk.toString().toUpperCase());
    },
  });
  const src: PassThrough = PassThrough.from('hello codeabc');
  let out = '';
  upper.on('data', (c: Buffer) => { out += c.toString(); });
  await pipeline(src, upper);
  console.log(out);
}
main();
Output
HELLO CODEABC

Language differences

PHP: stream filters are a PHP-unique mechanism — named filters (string.toupper, string.rot13, convert.base64-encode, zlib.*, ...) attach to a stream's read or write chain and transform every subsequent read/write; mode defaults from the open mode (r -> read chain, w -> write chain) and STREAM_FILTER_READ/WRITE/ALL override it (manual); stream_filter_prepend inserts at the chain head; the returned resource can be passed to stream_filter_remove.

Direction semantics: example one attaches to the write chain — bytes are uppercased on the way to disk; example two attaches to the read chain — raw bytes come back base64-encoded on the way out; production favourites include zlib.deflate for transparent compression and convert.iconv.* for transcoding (read chain = decode while reading).

Custom filters need stream_filter_register plus a php_user_filter subclass; data already sitting in internal buffers when a filter is attached is not affected (manual Note); an unknown filter name returns false with an E_WARNING.

C/C++: no filter concept in stdlib — transform explicitly after reading (hand-rolled toupper; base64 via third-party libs like OpenSSL).

C#: CryptoStream is the closest — it wraps a stream around an ICryptoTransform (the example uses ToBase64Transform), transforming on write.

Java: Base64.Encoder.wrap(OutputStream) is the isomorph — writes to the wrapper arrive encoded; Cipher's output-stream wrapping likewise acts as an encrypt/decrypt filter.

Go: no direct counterpart — encode explicitly with base64.StdEncoding after reading; a custom io.Writer wrapper achieves the equivalent pipeline.

JS/TS: Node's stream.Transform is the equivalent — chaining with pipeline() IS a filter chain; the example uppercases via a Transform.

Python: none — explicit base64/codecs calls; the codecs stream wrappers (StreamReader/Writer) are the legacy equivalent, rarely used in new code.

Rust: none in std — explicit to_uppercase() / the base64 crate; a custom filter is a hand-written io::Read/io::Write wrapper.

Paradigm gap: PHP hangs the transform on the stream (business code stays unaware, chains compose), while most languages put it in explicit calls (what you see is what runs, no hidden side effects) — hanging filters saves boilerplate but costs debuggability.