3v4l.org

run code in 300+ PHP versions simultaneously
<?php $input = "PHP CODING TECH"; $counters = array_fill_keys(explode(' ', $input), 1); // ['PHP' => 1, 'CODING' => 1, 'TECH' => 1] $bump = false; // permit outer loop to run while (!$bump) { // while still letters to output.... $bump = true; // stop after this iteration unless more letters to output foreach ($counters as $word => &$len) { // $len is mod-by-ref for incrementing echo substr($word, 0, $len); // echo letters using $len if ($bump && isset($word[$len])) { // if no $len has been incremented during inner loop... ++$len; // increment this word's $len $bump = false; // permit outer loop to run again } } echo "\n"; // separate outputs }

preferences:
36.15 ms | 406 KiB | 5 Q