3v4l.org

run code in 300+ PHP versions simultaneously
<?php ini_set('memory_limit', '2G'); set_time_limit(600); function bytesToSize($bytes, $precision = 2) { $symbols = array('B', 'Kb', 'Mb', 'Gb', 'Tb', 'Pb', 'Eb', 'Zb', 'Yb'); $exp = (int)floor(log($bytes) / log(1024)); return sprintf('%.'. $precision .'f '. $symbols[$exp], $bytes / pow(1024, floor($exp))); } function mem() { return bytesToSize(memory_get_usage(true)); } function test($msg, $test) { $t = microtime(true); $test($msg); echo $msg .': <b>'. sprintf('%.6f', microtime(true) - $t) .'</b><br>'; } $array = range(0, 3000000); test('foreach', function ($msg) use ($array) { echo $msg . ' before loop mem: '. mem() .'<br>'; foreach ($array as $v) { $b = $v; } echo $msg . ' after loop mem: '. mem() .'<br>'; }); test('foreach ref', function ($msg) use ($array) { echo $msg . ' before loop mem: '. mem() .'<br>'; foreach ($array as &$v) { $b = $v; } unset($v); echo $msg . ' after loop mem: '. mem() .'<br>'; }); test('for', function ($msg) use ($array) { echo $msg . ' before loop mem: '. mem() .'<br>'; $so = count($array); for ($i = 0; $i < $so; ++$i) { $b = $array[$i]; } echo $msg . ' after loop mem: '. mem() .'<br>'; }); test('while each -> list', function ($msg) use ($array) { echo $msg . ' before loop mem: '. mem() .'<br>'; while (list($k, $v) = each($array)) { $b =$v; } echo $msg . ' after loop mem: '. mem() .'<br>'; });
Output for 5.5.0 - 5.5.9
Fatal error: Out of memory (allocated 259784704) (tried to allocate 32 bytes) in /in/NT00B on line 22
Process exited with code 255.
Output for 5.4.15 - 5.4.25
Fatal error: Out of memory (allocated 260308992) (tried to allocate 32 bytes) in /in/NT00B on line 22
Process exited with code 255.
Output for 5.4.0 - 5.4.14
Fatal error: Out of memory (allocated 260571136) (tried to allocate 32 bytes) in /in/NT00B on line 22
Process exited with code 255.
Output for 5.3.0 - 5.3.28
Fatal error: Out of memory (allocated 265551872) (tried to allocate 32 bytes) in /in/NT00B on line 22
Process exited with code 255.
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17
Parse error: syntax error, unexpected T_FUNCTION in /in/NT00B on line 24
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_FUNCTION in /in/NT00B on line 24
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/NT00B on line 24
Process exited with code 255.

preferences:
108.46 ms | 401 KiB | 123 Q