Example 1 — Write Text to a File
Open a file, write a message with fputs(), and show how many bytes were written.
<?php
$file = __DIR__ . '/fputs_demo.txt';
$fp = fopen($file, 'w');
$bytes = fputs($fp, 'Hello');
fclose($fp);
echo "Wrote $bytes bytes", PHP_EOL;
echo 'File contains: ', file_get_contents($file), PHP_EOL;
unlink($file); How It Works
- fopen() in w mode opens the file for writing.
- fputs() writes the word Hello and returns 5, the number of bytes.
- fclose() saves and closes the file.
- file_get_contents() reads the file back to prove the text was saved.

