What is the purpose of the fgetcsv function in PHP and how can it be utilized in reading CSV files?

The fgetcsv function in PHP is used to read a line from a CSV file and parse it into an array. It can be utilized to easily read and process data stored in CSV files. By using fgetcsv, you can loop through the file line by line, extracting the values into an array for further manipulation.

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

while (($data = fgetcsv($file)) !== false) {
    // Process each line of the CSV file
    print_r($data); // Output the array of values for each line
}

fclose($file); // Close the file after reading