PHP fscanf() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, fscanf() reads one line from an open file and splits it into values using a format string.

The format works like the one in sscanf(). For example, %s reads a word and %d reads a whole number. It is a neat way to read simple text data that is separated by spaces.

Syntax

Call fscanf() with the values you need. Start with this line.

PHP
$row = fscanf($handle, "%s %d");
CallWhat it does
fscanf($fp, "%s %d")Returns an array with a word and a number from the next line.
fscanf($fp, "%s %d", $name, $age)Stores the values in $name and $age and returns how many were read.
fscanf($fp, "%d-%d-%d")Reads three numbers separated by dashes, such as a date.

Each call reads exactly one line. If you do not pass variables, you get an array. If you pass variables, they are filled in and the function returns the number of values assigned. Common format codes are %s (word), %d (integer), %f (decimal) and %c (one character).

Library declarations
PHP
fscanf(resource $stream, string $format, mixed &...$vars): array|int|false|null

Parameters

ParameterDescription
streamRequired. An open file handle returned by fopen().
formatRequired. The format string that describes the line, for example %s %d.
varsOptional. One or more variables passed by reference to receive the values.

Return Value

Return ValueDescription
arrayReturns an array of the parsed values when no variables are passed.
intReturns the number of values assigned when variables are passed.
falseReturns false at the end of the file or on failure.

PHP Examples

Example 1 — Read One Line into an Array

Read a line with a name, an age and a city, and print the parts.

PHP
<?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

  1. The file holds a single line with three values separated by spaces.
  2. %s reads a word, %d reads a whole number, and the last %s reads another word.
  3. fscanf() returns the three parts as an array.
  4. The age is already a real integer, so you can use it for maths straight away.

Example 2 — Read Until the End of the File

fscanf() returns false when there are no more lines, which makes it easy to loop.

PHP
<?php
$file = __DIR__ . '/scores.txt';
file_put_contents($file, "Asha 25\nRavi 31\nMeena 28\n");

$fp = fopen($file, 'r');

while ($row = fscanf($fp, '%s %d')) {
    [$name, $age] = $row;
    echo "$name is $age", PHP_EOL;
}

fclose($fp);
unlink($file);

How It Works

  1. Each loop pass reads one line and returns an array with a name and a number.
  2. [$name, $age] = $row splits the array into two variables.
  3. After the third line, fscanf() reaches the end of the file and returns false.
  4. False is not a truthy value, so the while loop stops by itself.

Example 3 — Read into Variables

Pass variables to fscanf() so the values are stored for you and you get a count back.

PHP
<?php
$file = __DIR__ . '/one.txt';
file_put_contents($file, "Asha 25\n");

$fp = fopen($file, 'r');
$count = fscanf($fp, '%s %d', $name, $age);
fclose($fp);

echo "$count values: $name is $age", PHP_EOL;

unlink($file);

How It Works

  1. $name and $age are passed after the format, and PHP fills them in.
  2. Because variables were given, fscanf() returns the number of values it assigned instead of an array.
  3. Two values were found, so $count is 2.
  4. echo prints the count and the values together.

Frequently Asked Questions

It reads one line from an open file and parses it according to a format string, returning the values as an array or storing them in variables.
It returns false, so you can use it as the condition of a while loop.
fscanf() reads a line from a file handle first and then parses it. sscanf() parses a string you already have.
%s stops at whitespace. For values with spaces, read the line with fgets() and split it with explode(), or use a CSV format with fgetcsv().
Yes. %d gives you an integer and %f gives you a float, so you do not need to cast them yourself.

Next: fseek()

fseek() moves the file pointer of an open file to a new position. The pointer is the invis.

fseek() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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