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) ) ); }

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.4.130.0120.01118.34
8.4.120.0050.00424.27
8.4.110.0120.00822.57
8.4.100.0150.00819.05
8.4.90.0090.00818.77
8.4.80.0060.00920.70
8.4.70.0100.00918.64
8.4.60.0050.00519.08
8.4.50.0120.00918.92
8.4.40.0140.00718.61
8.4.30.0090.01319.11
8.4.20.0180.00320.07
8.4.10.0090.00019.98
8.3.260.0120.00817.11
8.3.250.0110.01019.01
8.3.240.0130.00817.30
8.3.230.0100.00416.89
8.3.220.0120.00619.10
8.3.210.0050.00418.61
8.3.200.0060.00317.03
8.3.190.0040.00417.36
8.3.180.0090.00917.44
8.3.170.0130.00719.04
8.3.160.0040.01517.36
8.3.150.0130.00619.31
8.3.140.0120.00319.28
8.3.130.0050.00518.82
8.3.120.0060.00319.06
8.3.110.0060.00320.94
8.3.100.0040.00424.06
8.3.90.0120.00626.77
8.3.80.0000.01017.97
8.3.70.0150.00618.68
8.3.60.0100.00716.98
8.3.50.0240.00017.04
8.3.40.0150.00920.08
8.3.30.0180.00619.52
8.3.20.0140.01022.02
8.3.10.0120.00921.85
8.3.00.0130.01019.46
8.2.290.0080.01120.71
8.2.280.0100.01118.56
8.2.270.0000.01517.52
8.2.260.0160.00317.17
8.2.250.0040.00416.93
8.2.240.0070.00319.02
8.2.230.0060.00322.58
8.2.220.0030.00637.54
8.2.210.0040.00426.77
8.2.200.0040.00717.25
8.2.190.0100.01317.13
8.2.180.0060.00917.13
8.2.170.0200.00019.33
8.2.160.0170.00320.35
8.2.150.0150.00419.22
8.2.140.0150.00320.60
8.2.130.0110.00720.48
8.2.120.0190.00019.38
8.2.110.0120.00619.57
8.2.100.0070.01119.44
8.2.90.0100.01019.33
8.2.80.0130.00619.49
8.2.70.0060.01518.95
8.2.60.0130.01019.40
8.2.50.0120.00919.52
8.2.40.0140.01019.23
8.2.30.0130.01319.15
8.2.20.0100.01019.25
8.2.10.0120.00919.06
8.2.00.0100.00719.12
8.1.330.0120.00621.99
8.1.320.0080.01216.84
8.1.310.0060.01221.93
8.1.300.0100.01320.15
8.1.290.0040.00830.84
8.1.280.0160.00625.92
8.1.270.0030.01422.26
8.1.260.0100.00722.20
8.1.250.0060.01022.32
8.1.240.0120.00618.95
8.1.230.0100.00718.84
8.1.220.0130.00319.11
8.1.210.0100.00718.69
8.1.200.0130.00318.94
8.1.190.0160.00318.88
8.1.180.0120.00818.76
8.1.170.0040.01518.94
8.1.160.0070.01018.66
8.1.150.0100.00718.66
8.1.140.0170.00018.94
8.1.130.0100.01018.80
8.1.120.0130.00618.92
8.1.110.0090.00918.81
8.1.100.0070.01020.11
8.1.90.0030.01318.88
8.1.80.0070.01019.94
8.1.70.0120.00418.96
8.1.60.0060.01118.98
8.1.50.0100.00718.70
8.1.40.0070.01418.92
8.1.30.0150.00319.01
8.1.20.0100.00620.17
8.1.10.0070.01018.78
8.1.00.0100.00718.69

preferences:
35.07 ms | 403 KiB | 5 Q