What are some best practices for handling delimiters and enclosures in fputcsv?

When using fputcsv in PHP to write data to a CSV file, it is important to handle delimiters and enclosures properly to ensure that the data is formatted correctly. One common issue is when the data contains the delimiter or enclosure characters, which can cause the CSV file to be incorrectly parsed. To solve this issue, you can specify custom delimiter and enclosure characters when calling fputcsv.

$delimiter = '|';
$enclosure = '"';
$handle = fopen('data.csv', 'w');

$data = array(
    'John Doe',
    'johndoe@example.com',
    '555-555-5555',
);

fputcsv($handle, $data, $delimiter, $enclosure);

fclose($handle);