Example 1 — Read One Line into an Array
Read a line with a name, an age and a city, and print the parts.
<?php
$file = __DIR__ . '/people.txt';
file_put_contents($file, "Asha 25 Chennai\n");
$fp = fopen($file, 'r');
$row = fscanf($fp, '%s %d %s');
fclose($fp);
print_r($row);
unlink($file); How It Works
- The file holds a single line with three values separated by spaces.
- %s reads a word, %d reads a whole number, and the last %s reads another word.
- fscanf() returns the three parts as an array.
- The age is already a real integer, so you can use it for maths straight away.

