W3docs

fgetcsv()

The fgetcsv() function in PHP is used to read a line from a file and parse it as CSV (Comma-Separated Values) data. It's a crucial function for server

Introduction to PHP fgetcsv() Function

The fgetcsv() function in PHP is used to read a line from a file and parse it as CSV (Comma-Separated Values) data. It's a crucial function for web developers who need to read and process CSV files efficiently.

The fgetcsv() function accepts one required parameter and four optional parameters. In this article, we'll discuss the syntax and parameters of the fgetcsv() function, along with examples of how to use it.

Syntax

The syntax of the fgetcsv() function is as follows:

The Syntax of PHP fgetcsv()

array fgetcsv ( resource $stream [, int $length = 0 [, string $delimiter = ',' [, string $enclosure = '"' [, string $escape = '\\' ]]]] )
  • stream: the file pointer to read from
  • length: the maximum length of the line to read
  • delimiter: the delimiter character for CSV data
  • enclosure: the enclosure character for CSV data
  • escape: the escape character for CSV data

Parameters

The fgetcsv() function takes one required parameter and four optional parameters:

  1. $stream: The file pointer you want to read from. This parameter can be a resource created using the fopen() function or a similar function.
  2. $length: The maximum length of the line to read. This parameter is optional and defaults to 0, which means that the entire line will be read.
  3. $delimiter: The delimiter character for CSV data. This parameter is optional and defaults to ','.
  4. $enclosure: The enclosure character for CSV data. This parameter is optional and defaults to '"'.
  5. $escape: The escape character for CSV data. This parameter is optional and defaults to '\'. Note: This parameter is deprecated as of PHP 8.1.

Return Values

On success, fgetcsv() returns an indexed array containing the fields read from the line. If an error occurs or the end of the file is reached, it returns false.

Examples

Here are some examples of how to use the fgetcsv() function:

Example 1: Read a line of CSV data from a file

The following example opens a file, reads a line of CSV data, and properly closes the file handle:

Read a line of CSV data from a file in PHP

$fileHandle = fopen('data.csv', 'r');
if ($fileHandle) {
    $line = fgetcsv($fileHandle);
    fclose($fileHandle);
}

Example 2: Read a line of CSV data from a file with a custom delimiter

The following example reads a line of CSV data from the file pointer $fileHandle with a custom delimiter:

Read a line of CSV data from a file with a custom delimiter

$line = fgetcsv($fileHandle, 0, ';');

This code will read a line of CSV data with the ';' delimiter.

Conclusion

In conclusion, the fgetcsv() function is a crucial PHP function that can be used to read a line from a file and parse it as CSV data. It's essential for reading and processing CSV files efficiently.

By using the examples provided in this article, you should now be able to use the fgetcsv() function in your PHP code with ease. If you have any questions or concerns about using the fgetcsv() function in PHP, feel free to reach out to us. We'd be happy to help you out.

Practice

Practice

What is the function of fgetcsv() in PHP?