What is the function fgetcsv used for in PHP?

The function fgetcsv in PHP is used to read a line from a CSV file and parse it into an array. This function is useful when working with CSV files and allows for easy extraction of data in a structured format. By using fgetcsv, you can efficiently read and process CSV files line by line.

// Open the CSV file for reading
$csvFile = fopen('data.csv', 'r');

// Read and parse each line of the CSV file
while (($data = fgetcsv($csvFile)) !== false) {
    // Process the data from the CSV file
    print_r($data);
}

// Close the CSV file
fclose($csvFile);