A common use for fgetcsv is processing data without rebuilding the operation manually. Reads one CSV record from an open stream, returning fields as an array or false at end of file.
What Is fgetcsv in PHP?
Reads one CSV record from an open stream, returning fields as an array or false at end of file.
Syntax:
fgetcsv($stream, ??int $length = ..., ?string $separator = ..., ?string $enclosure = ..., ?string $escape = ...) : array|false
Parameters and Return Value
$stream— A required value input for this operation. Verify the allowed values in the examples and context below.$length— An optional ?int input for this operation. Verify the allowed values in the examples and context below.$separator— An optional string input for this operation. Verify the allowed values in the examples and context below.$enclosure— An optional string input for this operation. Verify the allowed values in the examples and context below.$escape— An optional string input for this operation. Verify the allowed values in the examples and context below.
Declared return type: array|false. Distinguish false, an empty result, and exceptions where the API permits them.
Example 1: Basic Usage
This example demonstrates the main operation with a small, controlled input.
<?php
$streamHandle = fopen("php://temp", "w+");
fwrite($streamHandle, "name,city\nAlex,Oslo\n");
rewind($streamHandle);
print_r(fgetcsv($streamHandle, 0, ",", '"', ''));
fclose($streamHandle);
The example focuses on the value produced or the state changed by this API. Review the return value and validate input before using this pattern in production.
Example 2: Another Input
This example changes the input or surrounding setup to illustrate another behavior.
<?php
$streamHandle = fopen("php://temp", "w+");
fwrite($streamHandle, "a;b;c\n");
rewind($streamHandle);
print_r(fgetcsv($streamHandle, 0, ";", '"', ''));
fclose($streamHandle);
The example focuses on the value produced or the state changed by this API. Review the return value and validate input before using this pattern in production.
Practical Example
This application-style example shows a self-contained usage pattern and checks its result.
<?php
$streamHandle = fopen("php://temp", "w+");
fwrite($streamHandle, "product,price\nbook,12\n");
rewind($streamHandle);
while (($rowValues = fgetcsv($streamHandle, 0, ",", '"', '')) !== false) { print_r($rowValues); }
fclose($streamHandle);
The example focuses on the value produced or the state changed by this API. Review the return value and validate input before using this pattern in production.
Important Behavior and Edge Cases
- In PHP 8.4 explicitly provide the escape argument; an empty string avoids PHP proprietary backslash escaping.
- Use the intended input type and handle empty or malformed values explicitly.
- Where this API requires an extension, version, or file, document that dependency before using it in a real application.
Comparison With Related Functions
| Function or Method | When to Use It |
|---|---|
fgetcsv | Reads one CSV record from an open stream, returning fields as an array or false at end of file. |
fputcsv | Writes a field array as one CSV record into a writable stream with quoting where needed. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Using this API without checking the required input or extension | In PHP 8.4 explicitly provide the escape argument; an empty string avoids PHP proprietary backslash escaping. |
| Splitting CSV on commas | Use fgetcsv() or fputcsv(), which handle quoted delimiters. |
| Relying on the implicit escape default | Pass an explicit escape argument, preferably an empty string for RFC-style CSV. |
Key Takeaways
- Reads one CSV record from an open stream, returning fields as an array or false at end of file.
- In PHP 8.4 explicitly provide the escape argument; an empty string avoids PHP proprietary backslash escaping.
- Test both the expected case and at least one failure or empty-input case before deployment.
Reads one CSV record from an open stream, returning fields as an array or false at end of file. Use it when the operation fits your input, output and validation requirements.