For completeness, here is a userspace str_putcsv() that is fully compatible with fgetcsv() and fputcsv()'s arguments. Namely $escape and $eol, which all others seem to be omitting.
<?php
function str_putcsv(
array $fields,
string $separator = ",",
string $enclosure = "\"",
string $escape = "\\",
string $eol = "\n"
) {
return implode($separator,
array_map(
function($a)use($enclosure, $escape) {
$type = gettype($a);
switch($type) {
case 'integer': return sprintf('%d', $a);
case 'double': return rtrim(sprintf('%0.'.ini_get('precision').'f', $a), '0');
case 'boolean': return ( $a ? 'true' : 'false' );
case 'NULL': return '';
case 'string':
return sprintf('"%s"', str_replace(
[$escape, $enclosure],
[$escape.$escape, $escape.$enclosure],
$a
));
default: throw new TypeError("Cannot stringify type: $type");
}
},
$fields
)
) . $eol;
}