3v4l.org

run code in 300+ PHP versions simultaneously
<?php $collection = [ 'A', 'B', 'C', 'D', 'E' ]; $offset = 4; $limit = 2; If($offset>count($collection) || $offset < 0 || $limit ==0) die("inputs out of bounds"); If($offset-$limit >=0){ $start = $offset-$limit; }else{ $start =0; } If($offset+$limit>count($collection)){ $end = count($collection)-$offset+$limit; }else{ $end = $offset + $limit; } If($start ==0) $end++; $result = array_slice($collection, $start, $end); Var_dump($result);

preferences:
25.81 ms | 405 KiB | 5 Q