Example 1 — Write, Rewind, Then Read
Write some text into memory and read it back from the start.
<?php
$handle = fopen('php://memory', 'w+');
fwrite($handle, 'Hello');
rewind($handle);
echo fread($handle, 5), PHP_EOL;
fclose($handle); How It Works
- php://memory opens a temporary stream that lives only in memory.
- fwrite() writes Hello, which moves the pointer to the end.
- rewind() puts the pointer back to position 0.
- fread() now reads the five characters from the start and echo prints them.

