3v4l.org

run code in 300+ PHP versions simultaneously
<?php $i = 0; function is_cyclic(array &$array) { global $i; // Exit when too deep. if (++$i > 10) exit; echo "Iteration $i:\n"; echo "Memory usage: " . memory_get_usage() . "\n"; echo "\n"; $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)) { //array_pop($array); return true; } } //array_pop($array); return false; } function craft_bomb() { $array = [range(1, 1000), [range(1, 512), 3]]; $array[1][1] = &$array; return $array; } $bomb = craft_bomb(); is_cyclic($bomb);
based on JSjts
Output for 8.0.0 - 8.0.1
Iteration 1: Memory usage: 448640 Iteration 2: Memory usage: 449120 Iteration 3: Memory usage: 518104 Iteration 4: Memory usage: 518512 Iteration 5: Memory usage: 571880 Iteration 6: Memory usage: 572288 Iteration 7: Memory usage: 641272 Iteration 8: Memory usage: 641680 Iteration 9: Memory usage: 695048 Iteration 10: Memory usage: 695456
Output for 7.4.0 - 7.4.14
Iteration 1: Memory usage: 448936 Iteration 2: Memory usage: 449416 Iteration 3: Memory usage: 518400 Iteration 4: Memory usage: 518808 Iteration 5: Memory usage: 572176 Iteration 6: Memory usage: 572584 Iteration 7: Memory usage: 641568 Iteration 8: Memory usage: 641976 Iteration 9: Memory usage: 695344 Iteration 10: Memory usage: 695752
Output for 7.3.0 - 7.3.26
Iteration 1: Memory usage: 448976 Iteration 2: Memory usage: 449440 Iteration 3: Memory usage: 510408 Iteration 4: Memory usage: 510808 Iteration 5: Memory usage: 560064 Iteration 6: Memory usage: 560464 Iteration 7: Memory usage: 621432 Iteration 8: Memory usage: 621832 Iteration 9: Memory usage: 671088 Iteration 10: Memory usage: 671488