I want to add a comma to every item except the last one. The last one must have \"and\".
Item 1, Item 2 and Item 3
But items can be from 1 +
So if on
Here’s a function for that; just pass the array.
function make_list($items) {
$count = count($items);
if ($count === 0) {
return '';
}
if ($count === 1) {
return $items[0];
}
return implode(', ', array_slice($items, 0, -1)) . ' and ' . end($items);
}
Demo