PHP fgetcsv() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, fgetcsv() reads one line from an open CSV file and splits it into an array of fields. It understands quotes, so a value like "Smith, John" stays in one piece.

Instead of cutting each line with explode() yourself, fgetcsv() parses the line for you and handles commas inside quoted values. Call it in a loop to read a whole CSV file row by row.

Syntax

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

PHP
$row = fgetcsv($handle, null, ',', '"', '');
CallWhat it does
fgetcsv($handle, null, ',', '"', '')Reads the next row and returns its fields as an array.
fgetcsv($handle, null, ';', '"', '')Reads a row from a file that uses semicolons as separators.
while (($row = fgetcsv($handle, null, ',', '"', '')) !== false) { ... }Reads every row of the file, one by one.

A blank line is returned as an array with a single null value, not as false. In PHP 8.4, relying on the default $escape value is deprecated, so the examples pass an empty string to turn the escape feature off.

Library declarations
PHP
fgetcsv(resource $stream, ?int $length = null, string $separator = ",", string $enclosure = "\"", string $escape = "\\"): array|false

Parameters

ParameterDescription
streamRequired. An open file stream, usually from fopen(), pointing at a CSV file.
lengthOptional. The longest line to read in bytes. The default null means no limit. In PHP 7 and earlier, 0 meant no limit.
separatorOptional. The field separator, exactly one character. The default is a comma.
enclosureOptional. The field quote character, exactly one character. The default is a double quote.
escapeOptional. The escape character. Pass an empty string to disable it. Relying on the default is deprecated in PHP 8.4.

Return Value

Return ValueDescription
arrayAn array with one item for each field of the row.
falseReturned at the end of the file or on an error.

PHP Examples

Example 1 — Read a CSV File Row by Row

Create a small CSV file with a header, read the header first and then print every data row.

PHP
<?php
$path = sys_get_temp_dir() . '/codetofun_fgetcsv.csv';
$csv = 'name,age' . PHP_EOL . 'Alice,30' . PHP_EOL . 'Bob,25' . PHP_EOL;
file_put_contents($path, $csv);

$handle = fopen($path, 'r');

$header = fgetcsv($handle, null, ',', '"', '');
echo 'Columns: ', implode(', ', $header), PHP_EOL;

while (($row = fgetcsv($handle, null, ',', '"', '')) !== false) {
    echo $row[0], ' is ', $row[1], ' years old', PHP_EOL;
}

fclose($handle);
unlink($path);

How It Works

  1. The first fgetcsv() call reads the header row and returns ['name', 'age'].
  2. implode() joins the header names so you can print them.
  3. The while loop calls fgetcsv() again for each data row.
  4. When the file ends, fgetcsv() returns false and the loop stops.

Example 2 — Blank Lines Return [null]

An empty line in the CSV file does not return false. It returns an array with one null value, which you should skip.

PHP
<?php
$path = sys_get_temp_dir() . '/codetofun_fgetcsv_edge.csv';
file_put_contents($path, 'a,b' . PHP_EOL . PHP_EOL . 'c,d' . PHP_EOL);

$handle = fopen($path, 'r');
$n = 0;

while (($row = fgetcsv($handle, null, ',', '"', '')) !== false) {
    $n++;
    echo $n, ': ', json_encode($row), PHP_EOL;
}

fclose($handle);
unlink($path);

How It Works

  1. The file has three lines, and the middle one is empty.
  2. For the empty line, fgetcsv() returns an array that holds a single null value.
  3. json_encode() prints the arrays so the [null] row is easy to see.
  4. In real code, skip rows where $row === [null] before using the fields.

Example 3 — Quoted Fields with Commas

When a value contains a comma, the CSV file wraps it in quotes. fgetcsv() keeps that value together.

PHP
<?php
$path = sys_get_temp_dir() . '/codetofun_fgetcsv_quoted.csv';
file_put_contents($path, '"Smith, John",Engineer' . PHP_EOL);

$handle = fopen($path, 'r');
$row = fgetcsv($handle, null, ',', '"', '');

echo 'Fields: ', count($row), PHP_EOL;
echo 'Name: ', $row[0], PHP_EOL;
echo 'Job: ', $row[1], PHP_EOL;

fclose($handle);
unlink($path);

How It Works

  1. The first value is wrapped in double quotes because it contains a comma.
  2. fgetcsv() understands the quotes and does not split inside them.
  3. count() shows that the row has two fields, not three.
  4. The quotes are removed from the returned value.

Frequently Asked Questions

It reads one line from an open CSV file and returns the fields of that line as an array. It correctly handles quoted values that contain commas.
Open the file with fopen() and call fgetcsv() inside a while loop that stops when it returns false. Close the file with fclose() at the end.
An array with a single null value, which is [null]. It does not return false, so skip such rows yourself.
Pass the separator as the third argument: fgetcsv($handle, null, ';', '"', ''). Many spreadsheets in Europe export files that way.
fgetcsv() reads from an open file and moves the pointer line by line, while str_getcsv() parses a CSV string you already have in memory.

Next: fgets()

fgets() reads one line from an open file. It returns the line as a string, including the l.

fgets() 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