Glossary

CSV

CSV (Comma-Separated Values) is a text format for tabular data: each line is a record, values separated by commas (or semicolons). The simplest data exchange format between Excel, databases, and applications.

Format

id,name,email,created_at
1,Alice,alice@example.com,2025-01-15
2,Bob,"Doe, Bob",2025-02-20

Values containing commas or quotes are wrapped in double quotes. Quotes inside a value are doubled: "".

PHP

// Reading
$file = fopen('users.csv', 'r');
while (($row = fgetcsv($file)) !== false) {
  [$id, $name, $email] = $row;
}
fclose($file);

// Writing
$file = fopen('export.csv', 'w');
fputcsv($file, ['id', 'name', 'email']); // header
fputcsv($file, [1, 'Alice', 'alice@example.com']);
fclose($file);

Pitfalls

Encoding: Excel opens files in Windows-1252 by default, not UTF-8. Add a BOM (\xEF\xBB\xBF) at the start of the file for Excel to display Cyrillic correctly.