3v4l.org

run code in 300+ PHP versions simultaneously
<?php function is_cyclic(array &$array, int $maxDepth = -1) { if ($maxDepth === 0) { throw new Exception('Maximum nesting level exceeded.'); } $lastKey = array_key_last($array); if ($lastKey === null) { // Array is empty return false; } static $marker; if ($marker === null) { $marker = new stdClass(); } if ($array[$lastKey] === $marker) { return true; } $array[] = $marker; foreach ($array as &$item) { if (is_array($item) && is_cyclic($item, $maxDepth - 1)) { array_pop($array); return true; } } array_pop($array); return false; } function craft_bomb() { $array = [1, [2, 3]]; $array[1][1] = &$array; return $array; } $bomb = craft_bomb(); var_export(is_cyclic($bomb, 10));
Output for 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.30, 8.2.0 - 8.2.25, 8.3.0 - 8.3.13
Fatal error: Uncaught Exception: Maximum nesting level exceeded. in /in/Nl7PZ:6 Stack trace: #0 /in/Nl7PZ(23): is_cyclic(Array, 0) #1 /in/Nl7PZ(23): is_cyclic(Array, 1) #2 /in/Nl7PZ(23): is_cyclic(Array, 2) #3 /in/Nl7PZ(23): is_cyclic(Array, 3) #4 /in/Nl7PZ(23): is_cyclic(Array, 4) #5 /in/Nl7PZ(23): is_cyclic(Array, 5) #6 /in/Nl7PZ(23): is_cyclic(Array, 6) #7 /in/Nl7PZ(23): is_cyclic(Array, 7) #8 /in/Nl7PZ(23): is_cyclic(Array, 8) #9 /in/Nl7PZ(23): is_cyclic(Array, 9) #10 /in/Nl7PZ(39): is_cyclic(Array, 10) #11 {main} thrown in /in/Nl7PZ on line 6
Process exited with code 255.

preferences:
67.49 ms | 407 KiB | 5 Q