How can special characters like semicolons be handled in CSV files in PHP?

Special characters like semicolons can be handled in CSV files in PHP by enclosing the fields containing special characters within double quotes. This ensures that the semicolons are treated as part of the field value and not as delimiters. Additionally, special characters can be escaped using the backslash (\) character to prevent any parsing issues.

$data = array(
    array('Name', 'Age', 'Email'),
    array('John Doe', 25, 'john.doe@example.com'),
    array('Jane Smith', 30, 'jane.smith@example.com;extra'),
);

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

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

fclose($fp);