3v4l.org

run code in 300+ PHP versions simultaneously
<?php function wordsToArr($str) { $words =[]; $ex_str =explode(' ',$str); foreach ($ex_str as $k=>$v) { $words[] =implode(' ',$ex_str); unset($ex_str[$k]); } return $words; } function wordsToArr2($str) { $words = [$str]; while ($pos =strpos ( $str , ' ')) { $str=substr($str,($pos+1)); $words[] =$str; } return $words; } function wordsToArr3($str) { $base = array_reverse(explode(' ', $str)); $words = [$placeholder = array_shift($base)]; foreach ($base as $word) { $words[] = $placeholder = $word . ' ' . $placeholder; } return array_reverse($words); } $sentence = 'The quick brown fox jumps over the lazy dog'; $now = microtime(true); for ($i=0;$i<100;$i++) { wordsToArr($sentence); } $end = microtime(true); echo 'implode '. $i .' times in: ' . number_format($end - $now, 8) . '/sec' . PHP_EOL; $now = microtime(true); for ($i=0;$i<100;$i++) { wordsToArr2($sentence); } $end = microtime(true); echo 'strpos '. $i .' times in: ' . number_format($end - $now, 8) . '/sec' . PHP_EOL; $now = microtime(true); for ($i=0;$i<100;$i++) { wordsToArr3($sentence); } $end = microtime(true); echo 'concat '. $i .' times in: ' . number_format($end - $now, 8) . '/sec' . PHP_EOL;

preferences:
30.2 ms | 412 KiB | 5 Q