3v4l.org

run code in 300+ PHP versions simultaneously
<?php function glue($array, $allButLast = ', ', $last = ' and ') { if (empty($array)) { return ''; } $glued = implode($allButLast, $array); $pos = strrpos($glued, $allButLast); if ($pos !== false) { $glued = substr_replace($glued, $last, $pos, strlen($allButLast)); } return $glued; } $array = [ 'bar', 'food', 'foo, bar', ]; echo glue($array);
Output for 8.1.0 - 8.1.34, 8.2.0 - 8.2.30, 8.3.0 - 8.3.30, 8.4.1 - 8.4.18, 8.5.0 - 8.5.3
bar, food, foo and bar

preferences:
66.37 ms | 982 KiB | 4 Q