3v4l.org

run code in 300+ PHP versions simultaneously
<?php $tests = [ '' => [], 'one' => ['one'], 'one and two' => ['one', 'two'], 'one, two and three' => ['one', 'two', 'three'], 'one, two, three and four' => ['one', 'two', 'three', 'four'], 'one, two, three, four and five' => ['one', 'two', 'three', 'four', 'five'], ]; function mickmackusa1(array $words, string $separator = ', ', string $conjunction = ' and '): string { return implode($separator, $words + ['last' => implode($conjunction, array_splice($words, -2))]); } function mickmackusa2(array $words, string $separator = ', ', string $conjunction = ' and ') { return implode($conjunction, [...(array) (implode($separator, array_splice($words, 0, -1)) ?: []), ...$words]); } function angryDan(array $words, $separator = ', ', $conjunction = ' and '): mixed { $last = array_pop($words); if ($words) { return implode($separator, $words) . $conjunction . $last; } return $last; } function deceze(array $words, string $separator = ', ', string $conjunction = ' and '): string { return join($conjunction, array_filter(array_merge(array(join(', ', array_slice($words, 0, -1))), array_slice($words, -1)), 'strlen')); } function jercSi(array $words, string $separator = ', ', string $conjunction = ' and '): string { $lastItem = array_pop($words); // c $text = implode($separator, $words); // a, b $text .= $conjunction . $lastItem; return $text; } function enrique(array $words, string $separator = ', ', string $conjunction = ' and '): mixed { $str = array_pop($words); if ($words) $str = implode($separator, $words) . $conjunction . $str; return $str; } function jJWright(array $words, string $separator = ', ', string $conjunction = ' and '): string { $last = array_pop($words); $remaining = count($words); return ($remaining ? implode(', ',$words) . $conjunction : '') . $last; } function sevavietl(array $words, string $separator = ', ', string $conjunction = ' and '): string { return implode( array_map( function ($item, $glue) { return $item . $glue; }, $words, array_slice(array_fill(0, count($words), $separator) + ['last' => $conjunction], 2) ) ); } function vision(array $words, string $separator = ', ', string $conjunction = ' and '): string { $words[] = implode($conjunction, array_splice($words, -2)); return implode($separator, $words); } function uruapanmexicansong(array $words, string $separator = ', ', string $conjunction = ' and '): string { $str = implode($separator, $words); if (count($words) == 2) { return str_replace($separator, $conjunction, $str); } return preg_replace("/[{$separator}](?!.*[{$separator}])/", $conjunction, $str); } function planetX(array $words, string $separator = ', ', string $conjunction = 'and '): string { ob_start(); foreach ($words as $key => $value) { if (count($words) - 1 == $key) echo $conjunction . $value; elseif (count($words) - 2 == $key) echo $value . " "; else echo $value . $separator; } return ob_get_clean(); } function alexRussell(array $words, string $separator = ', ', string $conjunction = 'and '): string { ob_start(); foreach ($words as $k => $model) { echo $model; if (!preg_match("/s|S$/", $model)) echo 's'; if (isset($words[$k + 1])) { echo $separator; if (!isset($words[$k + 2])) echo $conjunction; } } return ob_get_clean(); } function astha(array $words, string $separator = ', ', string $conjunction = ' and ') { $str = ""; $lenArr = sizeof($words); for($i = 0; $i < $lenArr; $i++) { if ($i == 0) $str .= $words[$i]; elseif ($i == ($lenArr-1)) $str .= $conjunction . $words[$i]; else $str .= $separator . $words[$i]; } return $str; } function zkent(array $words, string $separator = ', ', string $conjunction = 'and '): mixed { if (count($words) == 1) { return array_pop($words); } elseif (count($words) == 2) { return implode(" $conjunction", $words); } else { $last = array_pop($words); $list = implode($separator, $words); $list .= $separator . $conjunction . $last; return $list; } } function podolinek(array $words, string $separator = ', ', string $conjunction = ' and ') { if (count($words) === 0) { return ""; } elseif (count($words) === 1) { return $words[0]; } $firstItems = implode($separator, array_slice($words, 0, -1)); $lastItem = array_slice($words, -1)[0]; return $firstItems . $conjunction . $lastItem; } function greenButterfly(array $words, string $separator = ', ', string $conjunction = ' and '): string { return implode($separator, array_slice($words, 0, -1)) . $conjunction . array_slice($words, -1)[0]; } function josh(array $words, string $separator = ',', string $conjunction = ' and '): string { if (count($words) > 1) { return sprintf( '%s%s %s %s', implode($separator, array_slice($words, 0, -1)), count($words) > 2 ? $separator : '', $conjunction ?: '', array_pop($words) ); } return implode('', $words); } function jackB(array $words, string $separator = ', ', string $conjunction = 'and '): string { $words[count($words) - 1] = $conjunction . $words[count($words) - 1]; return implode($separator, $words); } function blazejKlisz(array $words, string $separator = ', ', string $conjunction = ' and '): string { return implode($conjunction, array_filter(array_reverse(array_merge(array(array_pop($words)), array(implode($separator, $words)))))); } function raviMisra(array $words, string $separator = ', ', string $conjunction = ' and '): string { if (empty($words)) { return ''; } $glued = implode($separator, $words); $pos = strrpos($glued, $separator); if ($pos !== false) { $glued = substr_replace($glued, $conjunction, $pos, strlen($separator)); } return $glued; } printf("| %20s| 0️⃣ | 1️⃣ | 2️⃣ | 3️⃣ | 4️⃣ | 5️⃣ |\n", 'user \ elements'); $funcs = [ 'mickmackusa1', 'mickmackusa2', 'deceze', 'jJWright', 'sevavietl', 'vision', 'astha', 'podolinek', 'blazejKlisz', 'raviMisra', 'angryDan', 'enrique', 'planetX', 'jercSi', 'zkent', 'uruapanmexicansong', 'josh', 'josh', 'alexRussell', 'greenButterfly', 'jackB' ]; foreach ($funcs as $func) { echo "--------------------------------------------------------------------\n"; printf( "| %20s| %s | %s | %s | %s | %s | %s |\n", $func, ...array_map( fn($result, $expected) => $result === $expected ? '✅': '💩', array_map($func, $tests), array_keys($tests) ) ); }

preferences:
26.12 ms | 418 KiB | 5 Q