Home PHP Stream stream_filter_prepend() PHP stream_filter_prepend() Function Definition In PHP, stream_filter_prepend() attaches a named filter at the beginning of a stream's filter chain so it runs before filters that were already there.
Use it when filter order matters. Unlike append, newly prepended filters do not reprocess data that was already sitting in internal buffers.
Syntax Call stream_filter_prepend() with the values you need. Start with this line.
$filter = stream_filter_prepend($stream, "string.toupper"); Call What it does stream_filter_prepend($fp, "string.toupper")Prepends uppercase using the default mode for the stream. stream_filter_prepend($fp, "string.rot13", STREAM_FILTER_READ)Puts ROT13 first on the read chain. stream_filter_prepend($fp, "string.tolower", STREAM_FILTER_WRITE)Puts lowercase first on the write chain.
Order is the main reason to choose prepend. If you only need one filter, append and prepend behave the same for new data. For custom filters, register them with stream_filter_register() first.
Library declarations stream_filter_prepend(resource $stream, string $filter_name, int $mode = 0, mixed $params = null): resource|false Parameters Parameter Description stream Required. The stream resource to filter. filter_name Required. Built-in or registered filter name. mode Optional. STREAM_FILTER_READ, STREAM_FILTER_WRITE, or STREAM_FILTER_ALL. params Optional. Extra parameters for filters that accept them.
Return Value Return Value Description resource|false A filter resource on success, or false on failure.
PHP Examples 1. Prepend string.toupper on Write 2. Show Prepend Order With Two Filters 3. ROT13 on Read With Prepend Example 1 — Prepend string.toupper on Write Attach string.toupper at the front of the write chain and write mixed-case text to php://memory.
<?php
$fp = fopen("php://memory", "w+");
stream_filter_prepend($fp, "string.toupper", STREAM_FILTER_WRITE);
fwrite($fp, "prepend me");
rewind($fp);
echo stream_get_contents($fp), PHP_EOL;
fclose($fp); How It Works A memory stream is opened. string. toupper is prepended for writes.fwrite() sends the mixed-case phrase through the filter. Reading back shows PREPEND ME. Example 2 — Show Prepend Order With Two Filters Prepend toupper after tolower is already attached, so toupper becomes first in the chain.
<?php
$fp = fopen("php://memory", "w+");
stream_filter_append($fp, "string.tolower", STREAM_FILTER_WRITE);
stream_filter_prepend($fp, "string.toupper", STREAM_FILTER_WRITE);
fwrite($fp, "AbC");
rewind($fp);
echo stream_get_contents($fp), PHP_EOL;
fclose($fp); How It Works tolower is appended first. toupper is then prepended, so it runs before tolower on write. Data becomes ABC, then abc. The final stored text is abc. Frequently Asked Questions What does stream_filter_prepend() do in PHP? It attaches a filter at the beginning of a stream's filter chain so the filter runs before filters already present.
When should I prepend instead of append? When the new filter must run first, or when you want the buffer behavior that does not reprocess already-buffered data.
Does prepend reprocess buffered data? No. Data already in internal buffers is not pushed through a newly prepended filter.
Can I prepend built-in filters? Yes. Names like string.toupper and string.rot13 work without registration.
How do I remove a prepended filter? Keep the resource returned by stream_filter_prepend() and pass it to stream_filter_remove().
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