Example 1 — Print a Whole File
Open a file and send all of its content to the output.
<?php
$file = __DIR__ . '/pass_demo.txt';
file_put_contents($file, 'Hello, CodeToFun!');
$fp = fopen($file, 'r');
$count = fpassthru($fp);
fclose($fp);
echo PHP_EOL, "Bytes sent: $count", PHP_EOL;
unlink($file); How It Works
- The file holds one short line of 17 characters.
- fpassthru() prints the file straight to the output without using a variable.
- It returns the number of bytes it sent, which is saved in $count.
- fclose() closes the handle, and echo prints the byte count on a new line.

