How can an array of strings be output as a list with the last value connected by "and" in PHP?

To output an array of strings as a list with the last value connected by "and" in PHP, you can use the `implode()` function to join the array elements with a comma and space. Then, you can replace the last comma with "and" using `strrpos()` to find the last occurrence of a comma and `substr_replace()` to replace it.

<?php
$array = array("apple", "banana", "cherry", "date");

$list = implode(", ", $array);
$lastComma = strrpos($list, ',');
if ($lastComma !== false) {
    $list = substr_replace($list, ' and', $lastComma, 2);
}

echo $list;
?>