What are some best practices for handling CSV files with line breaks within data entries in PHP?

When handling CSV files with line breaks within data entries in PHP, one best practice is to enclose each data entry in double quotes. This way, the line breaks within the data will not be interpreted as new lines in the CSV file. Additionally, using the PHP fputcsv function can help properly format the CSV file with line breaks in data entries.

<?php

$data = [
    ["John Doe", "25", "New York\nCity"],
    ["Jane Smith", "30", "Los Angeles"],
];

$fp = fopen('data.csv', 'w');

foreach ($data as $fields) {
    fputcsv($fp, $fields);
}

fclose($fp);