3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php $a = array(1,2,1,3,2,1,3,2,1,3,2,1,2,3,1,2,3); echo implode(" ", $a) . "\n"; $useSort = true; if ($useSort) { sort($a); } else { $first = 0; $last = sizeof($a) - 1; echo $last; for($i = $first; $i <= $last;) { echo $i . "\n"; $v = $a[$i]; echo "Found $v at index $i\n"; if ($v == 1) { echo "$v is low, swapping with value in index $first\n"; swap($a, $i, $first); $first += 1; } if ($v == 3) { echo "$v is high, swapping with value in index $last\n"; swap($a, $i, $last); $last -= 1; continue; } $i += 1; } } echo implode(" ", $a) . "\n"; function swap(&$arr, $k1, $k2) { echo "Swapping $k1 and $k2\n"; $tmp = $arr[$k2]; $arr[$k2] = $arr[$k1]; $arr[$k1] = $tmp; }
Output for 4.3.0 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
1 2 1 3 2 1 3 2 1 3 2 1 2 3 1 2 3 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3