How can the contents of a specific column in a CSV file be added using PHP?

To add the contents of a specific column in a CSV file using PHP, you can read the CSV file, loop through each row, and extract the value of the desired column. You can then sum up these values to get the total.

$csvFile = 'example.csv';
$columnIndex = 2; // Assuming the column index starts from 0
$total = 0;

if (($handle = fopen($csvFile, 'r')) !== FALSE) {
    while (($data = fgetcsv($handle)) !== FALSE) {
        $total += $data[$columnIndex];
    }
    fclose($handle);
}

echo 'Total of column ' . $columnIndex . ': ' . $total;