fgetcsv

FunctionParams and return types changed in PHP 8.0

Gets line from file pointer and parse for CSV fields.

PHP 5
PHP 7
PHP 8.0
Improved
PHP 8.1
PHP 8.2
PHP 8.3
PHP 8.4
Changed
PHP 8.5

fgetcsv Function synopsis

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

Parameters

$stream

A valid file pointer to a file successfully opened by fopen, popen, or fsockopen.

$length

OptionalType?intDefault valuenull

Must be greater than the longest line (in characters) to be found in the CSV file (allowing for trailing line-end characters). Otherwise the line is split in chunks of $length characters, unless the split would occur inside an enclosure.

$separator

OptionalTypestringDefault value","

The $separator parameter sets the field separator. It must be a single byte character.

$enclosure

OptionalTypestringDefault value"\""

The $enclosure parameter sets the field enclosure character. It must be a single byte character.

$escape

OptionalTypestringDefault value"\"

The $escape parameter sets the escape character. It must be a single byte character or the empty string. The empty string ("") disables the proprietary escape mechanism.

Return value

Typearray|false

Returns an indexed array containing the fields read on success, or false on failure.

A blank line in a CSV file will be returned as an array
comprising a single `null` field, and will not be treated
as an error.

Changes to the fgetcsv Function

PHP 8.4

PHP 8.0

  • Return type added: array|false
  • Parameter name of parameter #1 changed: $fp to $stream
  • Parameter type added for parameter #2 ($length): ?int
  • Parameter default value added for position #2 ($length): null
  • Parameter name of parameter #3 changed: $delimiter to $separator
  • Parameter type added for parameter #3 ($separator): string
  • Parameter default value added for position #3 ($separator): ","
  • Parameter type added for parameter #4 ($enclosure): string
  • Parameter default value added for position #4 ($enclosure): "\""
  • Parameter type added for parameter #5 ($escape): string
  • Parameter default value added for position #5 ($escape): "\\"
  fgetcsv(
-     $fp,
+     $stream,
-     $length,
+     ?int $length = null,
-     $delimiter,
+     string $separator = ",",
-     $enclosure,
+     string $enclosure = "\"",
-     $escape
+     string $escape = "\\"
-   )
+   ): array|false

fgetcsv Function Availability

PHP VersionAvailability
PHP 8.5Future Release Yes
PHP 8.4Upcoming Release Yes
PHP 8.3Supported (Latest) Yes
PHP 8.2Supported Yes
PHP 8.1Security-Fixes Only Yes
PHP 8.0Unsupported Yes
PHP 7.4Unsupported Yes
PHP 7.3Unsupported Yes
PHP 7.2Unsupported Yes
PHP 7.1Unsupported Yes
PHP 7.0Unsupported Yes
PHP 5.6Unsupported Yes
PHP 5.5Unsupported Yes
PHP 5.4Unsupported Yes
PHP 5.3Unsupported Yes